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

Clarified SeparateDatabaseAndState docs and added example of changing ManyToManyField. #12523

Conversation

adamchainz
Copy link
Sponsor Member

The SeparateDatabaseAndState documentation has always been too short and non-visual for how often one ends up using it (at least, I do). This adds an example and rewords it to point to RunSQL.state_operations and be less patronizing about avoiding it.

@adamchainz
Copy link
Sponsor Member Author

The example is drawn from a draft blog post I have - it's maybe not the best since it uses an empty database_operations, but it's the best use case I have off the top of my head.

Copy link
Member

@felixxm felixxm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adamchainz Thanks 👍 I would move an example to the howto/writing-migrations.txt (with a specific use case) and ref it in docs/ref/migration-operations.txt. I used migrations.SeparateDatabaseAndState few times and IMO moving from a custom to an auto-generated intermediate table for ManyToManyField is a better example because it uses both database_operations and state_operations, e.g. (sorry for a typos and not representative names)

        migrations.SeparateDatabaseAndState(
            database_operations=[
                # Rename a custom intermediate table to an auto-generated name.
                migrations.RunSQL(
                    '''
                        ALTER TABLE x RENAME TO y
                    ''',
                    '''
                        ALTER TABLE y RENAME TO x
                    ''',
                ),
            ],
            state_operations=[
                migrations.AlterUniqueTogether(
                    name='customintermediatemodel',
                    unique_together=set(),
                ),
                migrations.RemoveField(
                    model_name='customintermediatemodel',
                    name='foreign_key_1',
                ),
                migrations.RemoveField(
                    model_name='customintermediatemodel',
                    name='foreign_key_2',
                ),
                migrations.AlterField(
                    model_name='model1',
                    name='model_2_objs',
                    field=models.ManyToManyField(to='my_app.Model2'),
                ),
                migrations.DeleteModel(
                    name='CustomIntermediateModel',
                ),
            ],

What do you think?

@adamchainz
Copy link
Sponsor Member Author

Updated. I went the other way and used a migration to add the through model. Project: https://github.com/adamchainz/django-document-separate-database-and-state

@felixxm felixxm removed the request for review from andrewgodwin March 6, 2020 09:30
@adamchainz adamchainz force-pushed the document_separate_database_and_state branch from f47f5a1 to 3b4c3e1 Compare March 9, 2020 17:16
@felixxm felixxm changed the title Documented SeparateDatabaseAndState with an example Clarified SeparateDatabaseAndState docs and added example of changing ManyToManyField. Mar 10, 2020
@felixxm felixxm force-pushed the document_separate_database_and_state branch from 3b4c3e1 to d8fb929 Compare March 10, 2020 09:01
@felixxm
Copy link
Member

felixxm commented Mar 10, 2020

@adamchainz Thanks 👍 I pushed minor edits and added AuthorBook._meta.db_table tip.

@adamchainz adamchainz force-pushed the document_separate_database_and_state branch from d8fb929 to 263d70f Compare March 10, 2020 09:29
@adamchainz
Copy link
Sponsor Member Author

Thanks, I pushed a version of the advice on _meta.db_table into the text above, so it's alongside the advice where to get the new table name. I then expanded the comment in the migration to include where the old table name came from.

@felixxm felixxm force-pushed the document_separate_database_and_state branch from 263d70f to 62a0875 Compare March 10, 2020 09:40
Copy link
Member

@carltongibson carltongibson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super stuff. Thanks @adamchainz (and others).

docs/ref/migration-operations.txt Outdated Show resolved Hide resolved
… ManyToManyField.

Co-Authored-By: Mariusz Felisiak <felisiak.mariusz@gmail.com>
Co-Authored-By: Carlton Gibson <carlton.gibson@noumenal.es>
Co-Authored-By: René Fleschenberg <rene@fleschenberg.net>
@adamchainz adamchainz force-pushed the document_separate_database_and_state branch from 38075d1 to 2e08dc7 Compare March 10, 2020 09:54
@adamchainz
Copy link
Sponsor Member Author

Cheers all for review, I added you all as coauthors :)

@felixxm felixxm self-assigned this Mar 10, 2020
@felixxm
Copy link
Member

felixxm commented Mar 10, 2020

Merged in a9ee687.

@felixxm felixxm closed this Mar 10, 2020
@adamchainz adamchainz deleted the document_separate_database_and_state branch March 25, 2020 15:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants