简体   繁体   中英

Django migration error when changing manytomany field to Foreign key

Initially my models.py looked liked this

class P (models.Model):
    first_name=models.CharField(max_length=100);

    def __str__(self):
        return self.first_name

class T (models.Model):
    p_id=models.ManyToManyField(P)

Everything was working great. then i changed class T as below:

class T (models.Model):
        p_id=models.ForeignKey(P,on_delete=models.CASCADE,default=None,null=True)
#I have added default values coz django asks me to during migrations

I get following error:

django.db.utils.ProgrammingError: column "p_id_id" does not exist

Previously when such error happened I used to delete all tables from Postgresql DB, then comment out entire models.py in my app, run a fake migration, then uncomment models.py and run migrations again and it would work. But this destroys all data in DB. Is there a way to make it work without deleting any data?

The error I talk about in my question is perhaps coz i ran fake migration with p_id as as FK in models.py which told Django that p_id_id already exists in DB , just create a migration file. When i retried everything from scratch without fake migration the error disappeared.

Django wont change m2m to FK (one to many) automatically, and that makes sense. If i run migrations after changing m2m to FK , the mapping table appname_t_p gets deleted and data is lost forever. I did following and it worked:

Manually backed up mapping table created during m2m 'appname_t_p'.

create table atp as select * from appname_t_p;

change m2m to FK in models.py ( as shown in the question) . then ran migrations (not fake migration). This created a new all nulls column p_id_id in T table.

Then manually wrote sql update statement to add values from above backed-up table atp into p_id_id (all NULLS) column in table appname_t.

UPDATE appname_t t1
SET    p_id_id = t2.p_id
FROM   atp t2
WHERE  t2.t_id = t1.id;

Then verified values and it was right.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM