Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alembic does not detect models. #4

Closed
brotatos opened this issue Sep 18, 2013 · 17 comments
Closed

Alembic does not detect models. #4

brotatos opened this issue Sep 18, 2013 · 17 comments

Comments

@brotatos
Copy link

I set up Flask-Migrate using your suggested changes to __init__.py and initialized the migrations and attempted to generate a file. However, this migration showed no changes to the db even though I had clearly added a users table.

.
├── Procfile
├── README.md
├── TODO.md
├── bin
├── calpoly
├── config.pyc
├── flask_migrate
├── include
├── lib
├── manage.py
├── migrations
├── requirements.txt
├── run.py
└── runtime.txt

Here's the migration:

"""empty message

Revision ID: 2812f95cc1be
Revises: None
Create Date: 2013-09-17 23:05:53.508999

"""

# revision identifiers, used by Alembic.
revision = '2812f95cc1be'
down_revision = None

from alembic import op
import sqlalchemy as sa


def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    pass
    ### end Alembic commands ###


def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    pass
    ### end Alembic commands ###

And finally, the code.

@miguelgrinberg
Copy link
Owner

Does calling db.create_all() from a shell create your tables?

I think it won't either. It seems your model(s) aren't getting imported, so SQLAlchemy and Alembic do not see them.

@brotatos
Copy link
Author

Yes, doing a db.create_all() creates all of my tables via the shell.

Where would I have to import my models?

@miguelgrinberg
Copy link
Owner

Unfortunately I could not easily run your code to test it, it seems there are several things that need to be provided in the environment.

Models are typically imported in the app package, so basically in the __init__.py file.

@brotatos
Copy link
Author

Ah, sorry about the env issue. I'll work on that and get back to you.

@miguelgrinberg
Copy link
Owner

Just as a test, define a quick model in __init__.py, something like:

class Test(db.Model):
    id = db.Column(db.Integer, primary_key = True)
    name = db.Column(db.String(64))

and see if that is recognized by the migration.

@brotatos
Copy link
Author

Sure thing. Should I create the table before I generate the migration or would that even matter in this case?

@miguelgrinberg
Copy link
Owner

You know what, I think I know what's going on. Did you try deleting your database? If the database matches your model definition then Alembic will not detect any changes.

@brotatos
Copy link
Author

Success!

"""empty message

Revision ID: 4be2ca3324b2
Revises: None
Create Date: 2013-09-17 23:47:37.470241

"""

# revision identifiers, used by Alembic.
revision = '4be2ca3324b2'
down_revision = None

from alembic import op
import sqlalchemy as sa


def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.create_table('test',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sa.String(length=64), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )
    ### end Alembic commands ###


def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('test')
    ### end Alembic commands ###

@brotatos
Copy link
Author

I'll see if this works for my users module as well.

@miguelgrinberg
Copy link
Owner

If you want your user model to be in the migration you need to remove it from the database. Once you enable migrations all changes to the db need to be done by the migration scripts

@brotatos
Copy link
Author

Done. I imported my user models at the very bottom of the __init__.py with a simple from calpoly.users import models. However, I now end up with this error (after deleting my users table):

INFO  [alembic.migration] Context impl PostgresqlImpl.
INFO  [alembic.migration] Will assume transactional DDL.
Traceback (most recent call last):
  File "manage.py", line 4, in <module>
    manager.run()
  File "/home/robin/github/ieee/lib/python2.7/site-packages/flask_script/__init__.py", line 366, in run
    raise e
alembic.util.CommandError: Target database is not up to date.

@brotatos
Copy link
Author

This was alleviated by removing some already existing versions.

Thanks for all the help and the quick responses! 👍

@fananimi
Copy link

fananimi commented Nov 4, 2014

@brotatos I have same issue, But I dont' need to import models in the init.py file, I just need import views on the init.py and then in the views.py I import the models.

@iUwej
Copy link

iUwej commented Sep 11, 2017

@brotatos you need to run alembic upgrade head to avoid the alembic.util.CommandError: Target database is not up to date.
I suppose the tool requires you apply existing migrations before creating new ones

@notfresh
Copy link

One more tip, I met the same problem, but my solution is to check the module load order.

if your model definition python script,( such as XXX.py) is not in the app init load process, it will not find the model you create.

The case like, you import db to app.py and use db.init(app), but the model definition is ingnored.
so you better import the model-define python script into your app.py.

@push001
Copy link

push001 commented Jul 5, 2020

For my first Database migration. I was facing some error after "flask db migrate -m "users table". Then I set FLASK_APP to the value microblog.py Then error was resolved and got the output as yours.

My doubt: When I have followed all the steps for "automatic import of envirnoment variable" and created ".flaskenv" then why am I supposed to set FLASK_APP value manually. Am I missing something?

@miguelgrinberg
Copy link
Owner

When I have followed all the steps for "automatic import of envirnoment variable" and created ".flaskenv" then why am I supposed to set FLASK_APP value manually.

If you have python-dotenv installed in your environment and have a .flaskenv file, then there is no need to manually set FLASK_APP.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants