question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Fields deleted on migration

See original GitHub issue

Hello

I just tried an upgrade to django 1.8 form django 1.7.7 and from django-simple-history 1.5.4 to django-simple-history 1.6.1 and run into some problems when running new migrations.

I have several models with ForeignKey fields. After upgrading and running

./manege makemigrations

All the ForeignKey fields got removed and added again, causing data lost.

For instance one migrations does this (tables and fields in spanish):

    ...
    migrations.RemoveField(
        model_name='historicalitemordendecompra',
        name='item_id',
    ),
    ...
    migrations.AddField(
        model_name='historicalitemordendecompra',
        name='item',
        field=models.ForeignKey(null=True, db_constraint=False, blank=True, related_name='+', on_delete=django.db.models.deletion.DO_NOTHING, to='almacen.Item'),
    ),
    ...

I’ve been playing with the idea of manually changing migrations to AlterField and RenameField but that sounds like a lengthy and not sure how safe process.

Help is appreciated.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jmfedericocommented, Apr 28, 2015

Hello, I did almsot the same as you, but didn’t need to re-run “makemigrations”, I renamed and then altered the fields. I guess the result is the same:

  1. Create the autogenerated django migrations (i.e. python manage.py makemigrations)
  2. Edit the files: Replace all instances of ‘DeleteField’ with ‘RenameField’ changing name=“x_id” with old_name=“x_id” and adding new_name=“x”
  3. Edit the files: Replace all instances of ‘AddField’ to ‘AlterField’
  4. Verify that there are no DeleteField instances in the migration files… There shouldn’t be! Voila, all of your data should be good to migrate.
  5. Run python manage.py makemigrations to make sure NO new migrations are generated.
  6. Migrate the data on a test server (python manage.py migrate). Check to ensure everything’s moved over properly!

Cheers.

0reactions
rossmechaniccommented, Dec 14, 2018

Don’t think this issue is relevant anymore.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to drop columns using Rails migration - Stack Overflow
Generate a migration to remove a column such that if it is migrated ( rake db:migrate ), it should drop the column. And...
Read more >
Solved: Can't remove "Migrated On ..." field description
Follow a migration from JIRA Server to JIRA Cloud, there is an annoying field description which states that the field was migrated and...
Read more >
Removing fields with a Rails migration - Tosbourn
Ruby on Rails gives you powerful tools to make removing old fields easy, lets walk through how we can do this.
Read more >
Deleting a model and fields that relate to it results in ValueError
This seems to happen because a single migration is created which both deletes the model and the fields that reference it - but...
Read more >
Deleting/Editing fields with Migration Tool
Can I delete and edit custom fields using the ANT Migration Tool. In the documentation I only found how to delete the entire...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found