django-move-model
django-move-model copied to clipboard
sequences
Hi. Does it also rename sequences?
I'm not sure I understand. Could you please provide more info?
Let's say I have a model old_app.ModelA. Its primary key id points to sequence old_app_modela_id_seq.
localhost=# \d old_app_modela;
Table "public.old_app_modela"
Column | Type | Modifiers
-----------------+--------------------------+-------------------------------------------------------------------
id | integer | not null default nextval('old_app_modela_id_seq'::regclass)
I decide to move it to new_app. So the next value of primary key should still refer to same sequence, but it should be renamed to new_app_modela_id_seq, like this:
localhost=# \d new_app_modela;
Table "public.new_app_modela"
Column | Type | Modifiers
-----------------+--------------------------+-------------------------------------------------------------------
id | integer | not null default nextval('new_app_modela_id_seq'::regclass)
Basically, it should be done with statement:
ALTER SEQUENCE old_app_modela_id_seq RENAME TO new_app_modela_id_seq;
I see. No, it doesn't. Should it?
I think yes, it is convenient and more human readable.
Check this out, it may help you: http://marcela-campo.blogspot.sk/2015/01/django-migrations-moving-django-model.html
Thanks for the link. I read that article before implementing this. It doesn't account for other DB engines. For example, SQLite stores sequence information inside a sqlite_sequence table and ALTER SEQUENCE will raise a syntax error. There's also MySQL, Oracle, SQL Server, DB2, Firebird etc.
TBH, the scope of this project is to automate the method described at https://stackoverflow.com/a/30613732/1639699 and, while I understand and agree to the reasoning, renaming sequences seems beyond that.
Ideally renaming a sequence would be implemented as a db operation. Those, however, are not documented and currently I'm not sure how to do that.
One way to implement an operation would be to simply inherit either RunPython or RunSQL and run the appropriate query (or do nothing?) depending on the engine.