-
-
Notifications
You must be signed in to change notification settings - Fork 225
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
Comments
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. |
Yes, doing a db.create_all() creates all of my tables via the shell. Where would I have to import my models? |
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 |
Ah, sorry about the |
Just as a test, define a quick model in
and see if that is recognized by the migration. |
Sure thing. Should I create the table before I generate the migration or would that even matter in this case? |
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. |
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 ### |
I'll see if this works for my users module as well. |
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 |
Done. I imported my user models at the very bottom of the 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. |
This was alleviated by removing some already existing versions. Thanks for all the help and the quick responses! 👍 |
@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. |
@brotatos you need to run |
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. |
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? |
If you have python-dotenv installed in your environment and have a .flaskenv file, then there is no need to manually set FLASK_APP. |
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:
And finally, the code.
The text was updated successfully, but these errors were encountered: