sql - Reversing a 1:many relationship between two tables -
i have 2 mysql tables a , b. table a has member ref foreignkey reference b.id. makes 1:many association between b , a.
this has been in production few weeks, i'm adding more features code , have realized got original mapping wrong. needed many:1 relationship between b , a. is, b.ref should point a.id, not other way around. wasn't problem until because 1:1 mapping far. how migrate data new schema?
i'd guess:
alter table b add column ref integer constraint foreign key (a.id)-- add column first- run sql equivalent of "
for row in a: row.ref.ref = row" alter table drop column ref
attempting in sqlalchemy fails circular reference error. need in sql, not familiar necessary select+update syntax. help?
for step 2:
update b, set b.ref = a.id a.ref = b.id
Comments
Post a Comment