1

I've been confused,if there's any way how to order column when migrate in Django, In the first place In my case I don't have is_person in my models then eventually I want to add is_person after paid in my models like the example below in models.py, then the problem is even I correctly ordered the value in my models when migrate the is_person always goes to the last. Is there any trick how to add column after paid without deleting your whole database?

enter image description here

Models.py

class Person(models.Model):
    covid_id = models.CharField(max_length=50,blank=True, null=True)

    is_person = models.CharField(max_length=50,blank=True, null=True)  // I want to add this
    
    paid_by = models.CharField(max_length=50,blank=True, null=True) 
    date_receive = models.DateField(null=True, blank=True)
    remarks = models.CharField(max_length=1500,blank=True, null=True)
    eligible = models.CharField(max_length=50,blank=True, null=True)
    amount_paid = models.CharField(max_length=60,blank=True, null=True)
BootCamp
  • 444
  • 4
  • 18
  • 1
    No, Django does not care about column ordering, and for SQL server for example, to the best of my knowledge the only way to do this would be creating a new table with the right order and then "copy" the data from the old one: https://stackoverflow.com/questions/769828/add-a-new-table-column-to-specific-ordinal-position-in-microsoft-sql-server There are some databases that allow inserting columns on an arbitrary position, but anyway, it does not matter. If you switch the columns in the select statement it will fetch these in another order. – Willem Van Onsem Dec 09 '20 at 11:17

2 Answers2

1

Django does not care about the order of the columns, and some SQL databases do not seem to worry too much either. In some SQL dialects, like MySQL, you can indeed can specify the "insertion point" with:

ALTER table table_name ADD COLUMN my_column INTEGER AFTER other_column

But this is not standardized SQL. For example for SQL server, it looks like the only way to achieve this is by constructing a new table with the correct column order.

The order of the columns is also an "implementation detail". Databases are not meant to render output, these are meant to store, retrieve and aggregate data.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • thanks for your response but is this is the right way to add alter table? it return error to mysql `ALTER table app_person ADD COLUMN is_person VARCHAR(25) AFTER paid;` – BootCamp Dec 09 '20 at 11:55
  • It works now I will try,if my django app recognize – BootCamp Dec 09 '20 at 11:56
  • @MarkLogan: you will need to fake the migration where it adds the field, for example by altering the migration file where you initially constructed the table, since now it will of course aim to add an extra column. – Willem Van Onsem Dec 09 '20 at 11:57
  • but after I add alter table in mysql should necessary add `added_column` in models? then migrate? – BootCamp Dec 09 '20 at 11:58
  • @MarkLogan: Django does not look at a specific database to construct migration files (or migrate), since the migrations need to be applied both on the original database *and* a later production database. It only looks when it would run the migrations how such model would look like. It will thus determine that it still needs to add the other column. – Willem Van Onsem Dec 09 '20 at 12:00
  • thanks man, I just realized that ,it is not necessary use migration when you add another column in database – BootCamp Dec 09 '20 at 12:13
0

Do your migration with Django in the usual way, and then change the order of an existing column (by specifying the column's attributes as if it were an addition) as follows:

ALTER TABLE table_01 CHANGE COLUMN `column_05` `column_05` VARCHAR(255) NULL DEFAULT NULL AFTER `column_04`;