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:

  1. alter table b add column ref integer constraint foreign key (a.id) -- add column first
  2. run sql equivalent of "for row in a: row.ref.ref = row"
  3. 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

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -