Code Monkey home page Code Monkey logo

Comments (17)

miguelgrinberg avatar miguelgrinberg commented on May 23, 2024 22

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.

from flask-migrate.

miguelgrinberg avatar miguelgrinberg commented on May 23, 2024 20

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.

from flask-migrate.

miguelgrinberg avatar miguelgrinberg commented on May 23, 2024 8

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.

from flask-migrate.

notfresh avatar notfresh commented on May 23, 2024 5

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.

from flask-migrate.

miguelgrinberg avatar miguelgrinberg commented on May 23, 2024

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.

from flask-migrate.

brotatos avatar brotatos commented on May 23, 2024

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

Where would I have to import my models?

from flask-migrate.

brotatos avatar brotatos commented on May 23, 2024

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

from flask-migrate.

brotatos avatar brotatos commented on May 23, 2024

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

from flask-migrate.

brotatos avatar brotatos commented on May 23, 2024

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 ###

from flask-migrate.

brotatos avatar brotatos commented on May 23, 2024

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

from flask-migrate.

miguelgrinberg avatar miguelgrinberg commented on May 23, 2024

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

from flask-migrate.

brotatos avatar brotatos commented on May 23, 2024

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.

from flask-migrate.

brotatos avatar brotatos commented on May 23, 2024

This was alleviated by removing some already existing versions.

Thanks for all the help and the quick responses! 👍

from flask-migrate.

fananimi avatar fananimi commented on May 23, 2024

@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.

from flask-migrate.

iUwej avatar iUwej commented on May 23, 2024

@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

from flask-migrate.

push001 avatar push001 commented on May 23, 2024

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?

from flask-migrate.

miguelgrinberg avatar miguelgrinberg commented on May 23, 2024

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.

from flask-migrate.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.