开发者

Django Evolution: How do I add a new field at a specific position?

开发者 https://www.devze.com 2023-01-25 07:36 出处:网络
Is there a way to tell Django Evolution to add a new field to a database table at a specific position, similar to the AFTER statement of (My)SQL?

Is there a way to tell Django Evolution to add a new field to a database table at a specific position, similar to the AFTER statement of (My)SQL? I have a table with some columns and I want to add new_column after column1. Using SQL directly I would do this:

ALTER TABLE  `db_table` ADD  `new_column` DATETIME NULL DEFAULT NULL AFTER  `column1`

For Django Evolution this translates to:

MUTATIONS = [
    AddField('DbTable', 'new_column', models.DateTimeField, null=True, ...)
]

However, this would add new_column to the end of the table, is there something I can pass in for the dots in the above statement giving more control over t开发者_如何学Gohe order?


Django does not depend or care about the column position in the database. My guess is that Django Evolution works the same way.

So you shouldn't depend or care too much about database internals either, in the end that is why you are using the ORM, right?

If you really have to do it, I suggest subclassing django_evolution.mutations.AddField and overwriting the add_column method to integrate the AFTER statement in the list of SQL statements this method returns. There might be better places, this would be a hack.

0

精彩评论

暂无评论...
验证码 换一张
取 消