use_alter doesn't work for ForeignKeyConstraint

Issue #503 resolved
Former user created an issue

The code demonstrating the use_alter keyword found on:

http://www.sqlalchemy.org/trac/wiki/New03Tricks

doesn't work if I convert the ForeignKey to a table ForeignKeyConstraint, as follow:

from sqlalchemy import *

meta = BoundMetaData('sqlite:///')

t1 = Table('table1', meta,
    Column('id', Integer, primary_key=True),
    Column('t2_id', Integer, ForeignKey('table2.id'))
)

t2 = Table('table2', meta,
    Column('id', Integer, primary_key=True),
#    Column('t1_id', Integer, 
#           ForeignKey('table1.id', use_alter=True, name='t2id_fk'))
    Column('t1_id', Integer),
    ForeignKeyConstraint(['t1_id']('t1_id'), ['table1.id']('table1.id'), 
                         use_alter=True, name='t2id_fk')
)

meta.create_all()

The result is a FlushError, as follows:

sqlalchemy.exceptions.FlushError: Circular dependency detected [table1), (table1, table2)]((table2,)[]

By the way, a FlushError in this case, is a bit misleading since there was no flush yet.

I've found a way to fix this, but I'm not sure it's the correct way: in the append_element method of ForeignKeyConstraint, change the following line:

fk = ForeignKey(refcol, constraint=self)

to:

fk = ForeignKey(refcol, constraint=self, use_alter=self.use_alter)

Comments (1)

  1. Log in to comment