Changing a ForeignKey to a be the parent class/model

Viewed 14

I have a situation where there are various entities, with ForeignKeys to models that are subclasses of another model. I need to update these FKs to point to the parent, so that any of the various subclasses could be linked.

For example, the code might be:

class School(Model):
    ...

class University(School):
    ...

class Student(Model):
    university = ForeignKey(University)

... and I need to change that FK from Student to University, to be an FK to School.

I think that all Universities will have the same ID as the School that is in the database for that university. So is it safe/reliable to define the Student:

class Student(Model):
    university = ForeignKey(University)
    school = ForeignKey(School)

Then make a migration that creates the new school attribute, and uses RunPython to copy the id from university into school, then delete the university attribute and makemigrations for that?

Is it ever possible that this method would break, or produce bad data?

0 Answers
Related