diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2007-09-08 21:09:50 +0000 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2007-09-08 21:09:50 +0000 |
commit | fbd0a0c725249ef22260a3714ed564a191c0c45c (patch) | |
tree | 5af227e092890998774856e5ca6eaa9189a798f5 /test/engine/reflection.py | |
parent | dd162c4b30963968178d43adb766ac4355636c80 (diff) | |
download | sqlalchemy-fbd0a0c725249ef22260a3714ed564a191c0c45c.tar.gz |
[ticket:728] foreign key checks for existing reflected FK and replaces itself
Diffstat (limited to 'test/engine/reflection.py')
-rw-r--r-- | test/engine/reflection.py | 47 |
1 files changed, 43 insertions, 4 deletions
diff --git a/test/engine/reflection.py b/test/engine/reflection.py index c70dc3f2e..1f2b4f56a 100644 --- a/test/engine/reflection.py +++ b/test/engine/reflection.py @@ -125,8 +125,10 @@ class ReflectionTest(PersistTest): finally: meta.drop_all() - def testoverridecolumns(self): - """test that you can override columns which contain foreign keys to other reflected tables""" + def test_override_create_fkcols(self): + """test that you can override columns and create new foreign keys to other reflected tables. + this is common with MySQL MyISAM tables.""" + meta = MetaData(testbase.db) users = Table('users', meta, Column('id', Integer, primary_key=True), @@ -144,7 +146,10 @@ class ReflectionTest(PersistTest): autoload=True) u2 = Table('users', meta2, autoload=True) - assert len(a2.c.user_id.foreign_keys)>0 + assert len(a2.c.user_id.foreign_keys) == 1 + assert len(a2.foreign_keys) == 1 + assert [c.parent for c in a2.foreign_keys] == [a2.c.user_id] + assert [c.parent for c in a2.c.user_id.foreign_keys] == [a2.c.user_id] assert list(a2.c.user_id.foreign_keys)[0].parent is a2.c.user_id assert u2.join(a2).onclause == u2.c.id==a2.c.user_id @@ -176,7 +181,7 @@ class ReflectionTest(PersistTest): finally: meta.drop_all() - def testoverridecolumns2(self): + def test_override_fkandpkcol(self): """test that you can override columns which contain foreign keys to other reflected tables, where the foreign key column is also a primary key column""" meta = MetaData(testbase.db) @@ -222,6 +227,40 @@ class ReflectionTest(PersistTest): finally: meta.drop_all() + def test_override_existing_fkcols(self): + """test that you can override columns and specify new foreign keys to other reflected tables, + on columns which *do* already have that foreign key, and that the FK is not duped. + """ + + meta = MetaData(testbase.db) + users = Table('users', meta, + Column('id', Integer, primary_key=True), + Column('name', String(30)), + test_needs_fk=True) + addresses = Table('addresses', meta, + Column('id', Integer,primary_key=True), + Column('user_id', Integer, ForeignKey('users.id')), + test_needs_fk=True) + + + meta.create_all() + try: + meta2 = MetaData(testbase.db) + a2 = Table('addresses', meta2, + Column('user_id',Integer, ForeignKey('users.id')), + autoload=True) + u2 = Table('users', meta2, autoload=True) + + assert len(a2.foreign_keys) == 1 + assert len(a2.c.user_id.foreign_keys) == 1 + assert len(a2.constraints) == 2 + assert [c.parent for c in a2.foreign_keys] == [a2.c.user_id] + assert [c.parent for c in a2.c.user_id.foreign_keys] == [a2.c.user_id] + assert list(a2.c.user_id.foreign_keys)[0].parent is a2.c.user_id + assert u2.join(a2).onclause == u2.c.id==a2.c.user_id + finally: + meta.drop_all() + def test_pks_not_uniques(self): """test that primary key reflection not tripped up by unique indexes""" testbase.db.execute(""" |