Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 the order?

like image 782
friederbluemle Avatar asked Nov 19 '25 06:11

friederbluemle


1 Answers

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.

like image 171
stefanw Avatar answered Nov 21 '25 18:11

stefanw



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!