Discussion:
Migrations - moving model between apps
Matthew Pava
2018-11-15 23:09:03 UTC
Permalink
I'm moving a model to a different app in my project, and I want to keep the same data that I already have. I created a migration with a RunSQL operation that simply renames the table with the new app prefix. However, when I run makemigrations, Django insists on adding a migrations.CreateModel operation for me in the new app. How do I avoid that?
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4959b2480e4044d8b48a74b22457d7a4%40iss2.ISS.LOCAL.
For more options, visit https://groups.google.com/d/optout.
Simon Charette
2018-11-16 00:33:18 UTC
Permalink
Hello Matthew,

I'm going to assume no foreign keys are pointing to the model "Foo" you are
trying to move from the app "old" to "new". If it's the case then simply
repoint
them and run makemigrations once the following steps are completed. You
could wrap the generated AlterField in SeparateDatabaseAndState to avoid
database level foreign key constraints reconstruction if it's critical to
your
application.

Start by setting old.Foo.Meta.db_table = 'new_foo' and run makemigrations
old.

That should generate you a new migration with an AlterModelTable('foo',
'new_foo') operation.

Within this migration you'll want to add a DeleteModel('foo') operation
wrapped in a
SeparateDatabaseAndState[0] after the AlterModelTable one. Your migration's
operations
should look like:

operations = [
AlterModelTable('foo', 'new_foo'),
SeparateDatabaseAndState([], [DeleteModel('foo')]),
]

Now, move your "old.Foo" model definition to your "new" app, remove the
new.Foo.Meta.db_table and run "makemigrations new".

Edit the created "new" migration and wrap the CreateModel into a
SeparateDatabaseAndState
and make sure the migration depends on the "old" migration you just
created. You migration
should look like the following:

dependencies = [
('old', '000N_old_migration_name'),
]

operations = [
SeparateDatabaseAndState([], [
CreateModel('foo, ...)
)]),
]

Note that this will not take care of renaming the content types and
permissions that
might have been created for your old.Foo model definition. You could use a
RunPython
operation to rename them appropriately.

Cheers,
Simon
I’m moving a model to a different app in my project, and I want to keep
the same data that I already have. I created a migration with a RunSQL
operation that simply renames the table with the new app prefix. However,
when I run makemigrations, Django insists on adding a
migrations.CreateModel operation for me in the new app. How do I avoid
that?
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/20606f64-15b4-4946-bb3e-65276de63e40%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Matthew Pava
2018-11-16 15:12:38 UTC
Permalink
Hi Simon,
Thank you so very much for your assistance.
It was a very simple model, so it had no foreign keys, and I’m relieved that I won’t have to do some manual database operations to get this working.
It has migrated successfully!
Thank you,
Matthew

From: django-***@googlegroups.com [mailto:django-***@googlegroups.com] On Behalf Of Simon Charette
Sent: Thursday, November 15, 2018 6:33 PM
To: Django users
Subject: Re: Migrations - moving model between apps

Hello Matthew,

I'm going to assume no foreign keys are pointing to the model "Foo" you are
trying to move from the app "old" to "new". If it's the case then simply repoint
them and run makemigrations once the following steps are completed. You
could wrap the generated AlterField in SeparateDatabaseAndState to avoid
database level foreign key constraints reconstruction if it's critical to your
application.

Start by setting old.Foo.Meta.db_table = 'new_foo' and run makemigrations old.

That should generate you a new migration with an AlterModelTable('foo', 'new_foo') operation.

Within this migration you'll want to add a DeleteModel('foo') operation wrapped in a
SeparateDatabaseAndState[0] after the AlterModelTable one. Your migration's operations
should look like:

operations = [
AlterModelTable('foo', 'new_foo'),
SeparateDatabaseAndState([], [DeleteModel('foo')]),
]

Now, move your "old.Foo" model definition to your "new" app, remove the
new.Foo.Meta.db_table and run "makemigrations new".

Edit the created "new" migration and wrap the CreateModel into a SeparateDatabaseAndState
and make sure the migration depends on the "old" migration you just created. You migration
should look like the following:

dependencies = [
('old', '000N_old_migration_name'),
]

operations = [
SeparateDatabaseAndState([], [
CreateModel('foo, ...)
)]),
]

Note that this will not take care of renaming the content types and permissions that
might have been created for your old.Foo model definition. You could use a RunPython
operation to rename them appropriately.

Cheers,
Simon

Le jeudi 15 novembre 2018 18:09:45 UTC-5, Matthew Pava a écrit :
I’m moving a model to a different app in my project, and I want to keep the same data that I already have. I created a migration with a RunSQL operation that simply renames the table with the new app prefix. However, when I run makemigrations, Django insists on adding a migrations.CreateModel operation for me in the new app. How do I avoid that?
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+***@googlegroups.com<mailto:django-users+***@googlegroups.com>.
To post to this group, send email to django-***@googlegroups.com<mailto:django-***@googlegroups.com>.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/20606f64-15b4-4946-bb3e-65276de63e40%40googlegroups.com<https://groups.google.com/d/msgid/django-users/20606f64-15b4-4946-bb3e-65276de63e40%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/59362e8a8331457b85670b56eb4e2d2b%40iss2.ISS.LOCAL.
For more options, visit https://groups.google.com/d/optout.
Loading...