Migrating from ManyToOne (ForeignKey) into OneToOne in django codebase

53 views
Skip to first unread message

Ngoc Dung Hoang

unread,
Mar 6, 2023, 1:19:13 PM3/6/23
to Django users
Hello everyone,

I'm working on the system with 2 very big tables (billion of records) that is having ManyToOne relationship with other. The size of these tables are nearly the same.

Lets say I have table child and parent. Each of the parent can have multiple child (children) records.

The child table is using `parent = models.ForeignKey(Parent, ....)` declaration.

The idea is to change the relationship into "a parent will only have one children, with all the legacy records existing."

From database constraint level, I can do it pretty easy by using conditional check/uniq/ constraint. Even the existing indices (django indices) can already being used. I also create a new child_id (null for legacy record, uniq for new record) column for parent table, to have easier control on those constraints condition. This I believed it would be helpful for the django part below.

The django part is what I would love look for opinions.
 - I can keep the models as it is and enforce some convention rule/supporting method to have usage of one-to-one relationship. This seem to be ugly and we tent to live with the legacy records for quite some times.

- Extend the foreignkey class or its relatives/parent class, so that it can "behave" as close as OneToOne (for new records) and ManyToOne (for old records).

I digged a bit into the source code, I think my jackpot would be looking into the Related classes and Descriptor classes. But im pretty far from finish getting whole picture.

My wet dream would be something:

a_parent.child might got exception if it is legacy record (or pick the newest child, this part not that hard)
a_parent.children still yield list of child (easier on refactoring with small steps)
child1.parent = _id1; child2.parent = _id1 still work (for legacy, parent.child_id is Null)
parent.child = child3; parent.save(); child4.parent = parent not work (new, parent.child_id is not Null, probably depended on my db constraints)

Really appreciate if anyone have any opinions or hint/direction around this topic.

Thanks bunch.

Andy (Dung), Hoang



Reply all
Reply to author
Forward
0 new messages