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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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.

Reply via email to