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

Run "Create" without changes breaks the code #10

Closed
faanbece opened this issue Aug 3, 2019 · 3 comments
Closed

Run "Create" without changes breaks the code #10

faanbece opened this issue Aug 3, 2019 · 3 comments

Comments

@faanbece
Copy link

faanbece commented Aug 3, 2019

When you create a migration for the first time it does it correctly. Then if you make changes somewhere and create the migration and update the database, the unmodified tables detect them as new.

Example:
 
Create the model (models.py):

class User (db.Model):
    username = CharField (unique = True)

class Tweet (db.Model):
    user = ForeignKeyField (User, backref = 'tweets')
    timestamp = DateTimeField (default = datetime.datetime.now)

Create the migration (console)
flask-db create app.models
and you get:
INFO: created: 0001_create_table_user
INFO: created: 0002_create_table_tweet

Apply the migration to the DB (console):
flask-db upgrade
and you get
INFO: all migrations applied!

Now, you want to make a change in the model (models.py)

class Tweet (db.Model):
    user = ForeignKeyField (User, backref = 'tweets')
    timestamp = DateTimeField (default = datetime.datetime.now)
    content = TextField ()

Create the new migration
flask-db create app.models
 and you get:
INFO: created: 0003_create_table_user
INFO: created: 0004_create_table_tweet

Apply new migration to the DB:
flask-db upgrade
and get
INFO: upgrade: 0003_create_table_user
ERROR: table "user" already exists

@timster
Copy link
Owner

timster commented Aug 5, 2019

Yes, that's the right functionality.

Running create generates the initial create_table commands but it does not have the ability to detect changes in models.

If you add a column to your model, you'll need to generate a new migration and add the add_column statement manually.

flask db revision "add content to tweet"

Then update your migration:

def upgrade(migrator):
    migrator.add_column('tweet', 'content', 'text')

def downgrade(migrator):
    migrator.drop_column('tweet', 'content')

@timster timster closed this as completed Aug 7, 2019
@faanbece
Copy link
Author

faanbece commented Aug 9, 2019

  • Are there plans to implement this feature in the future?
  • This software is considered complete as is?
  • Or in case I can contribute to the development of this feature, will my pull request be accepted?

@timster
Copy link
Owner

timster commented Aug 9, 2019

I think it would be a great feature to have. I have no plans to implement it, mostly because I don't have time to dedicate to it.

I have given it much thought though. There are many different changes to a model that could take place like...

  1. adding a column
  2. removing a column
  3. renaming a column
  4. changing column data type
  5. changing column length
  6. and more...

Django and Alembic have both solved this, but using different strategies. Django keeps track of model history (it knows what your models look like before each change). I wouldn't be opposed to doing something like that, but it's definitely more complex.

Adding a column and removing a column are the easiest to detect and quite common actions, so even if it were able to do that, I think it would be helpful.

I will gladly accept pull requests for this feature (or any other feature for that matter). I don't use this library too often anymore, but if it's useful to other people I'd like to keep it alive. I'd be glad to help with development as well, I just don't have time to do it completely in any reasonable time frame.

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

2 participants