Wednesday, March 15, 2017

Re: Rename Table limiting the down of the system

On 15/03/2017 8:14 PM, Stefano Tranquillini wrote:
> Thanks,
> On Wed, Mar 15, 2017 at 9:43 AM, Mike Dewhirst <miked@dewhirst.com.au
> <mailto:miked@dewhirst.com.au>> wrote:
>
> On 15/03/2017 7:12 PM, Stefano Tranquillini wrote:
>
> Hi people.
>
> I've to do a migration for the database, specifically I've to
> rename a table.
>
> To do so I've a RunPython that runs this sql statemnet
> `ALTER TABLE "old_table" RENAME TO "new_table"`
> First question: should I use
> https://docs.djangoproject.com/en/1.8/ref/migration-operations/#renamemodel
> <https://docs.djangoproject.com/en/1.8/ref/migration-operations/#renamemodel>
> , does it do the same?
>
>
> I believe it does the same ... certainly worked for me with
> Postgres. Make an empty migration.
>
> operations = [
>
> migrations.RenameModel('Oldname', 'Newname'),
>
> migrations.AlterModelTable('Newname', 'appname_newname'),
>
> ]
>
>
> ​the fact is that when i do renameModel it tells me that "No installed
> app with label.." but if run the sql statment as above it works.

That is reasonable. I think the SQL is just executed as is. The
migration system permits you to run anything because it trusts you.

> in your operations there is also the alter model, why so?​

Because RenameModel only renames the model. It leaves the table
untouched. It is like you change the model name to make the design more
realistic for future maintainers of the code but assume they will never
look at the database.

Without also altering the table name in the database you would need a
meta property that says db_table=oldtablename

https://docs.djangoproject.com/en/1.8/ref/models/options/#db-table

So first rename the model name and then alter the newly named model to
match.

>
> Will these operation update all the refrences in the code,
> such that django will keep working without problems?
>
>
> Definitely not.
>
>
> ​I probably asked the worng question. I was trying to infeer if
> references in the DB will be updated as well.​

I believe so, but you will have to study the source code of
AlterTableName ...

https://docs.djangoproject.com/en/1.8/_modules/django/db/migrations/operations/models/#AlterModelTable

Cheers

Mike

>
>
> --
> Stefano
> --
> 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+unsubscribe@googlegroups.com
> <mailto:django-users+unsubscribe@googlegroups.com>.
> To post to this group, send email to django-users@googlegroups.com
> <mailto: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/CAPQ1%3DkCRp5XFGzRmCUE7H1Hq%3DPD1_MAkw3kND%2BXD-m0%2BexDL-Q%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAPQ1%3DkCRp5XFGzRmCUE7H1Hq%3DPD1_MAkw3kND%2BXD-m0%2BexDL-Q%40mail.gmail.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+unsubscribe@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/30fa6326-e8f1-d1f1-198c-33d8e9cc2b1c%40dewhirst.com.au.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment