From 23b744807599d01e6652a83d82e29da95695c814 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sun, 8 Jan 2006 18:11:52 +0000 Subject: [PATCH] sqlite/postgres reflection will properly add foreign keys added append_item() method to column to work similarly to table.append_item(), used to append foreign keys to the column (required in mysql) appending new foreign keys will properly replace the old one, so explicitly appending foreign keys to tables will replace those loaded via table reflection (instead of doubling them up) --- lib/sqlalchemy/databases/information_schema.py | 3 ++- lib/sqlalchemy/databases/sqlite.py | 2 +- lib/sqlalchemy/schema.py | 7 +++++++ test/engines.py | 13 ++++++++++--- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/lib/sqlalchemy/databases/information_schema.py b/lib/sqlalchemy/databases/information_schema.py index fdd705ed2e..dae3af2c69 100644 --- a/lib/sqlalchemy/databases/information_schema.py +++ b/lib/sqlalchemy/databases/information_schema.py @@ -130,4 +130,5 @@ def reflecttable(engine, table, ischema_names, use_mysql=False): table.c[constrained_column]._set_primary_key() elif type=='FOREIGN KEY': remotetable = Table(referred_table, engine, autoload = True, schema=referred_schema) - table.c[constrained_column].foreign_key = schema.ForeignKey(remotetable.c[referred_column]) + table.c[constrained_column].append_item(schema.ForeignKey(remotetable.c[referred_column])) + diff --git a/lib/sqlalchemy/databases/sqlite.py b/lib/sqlalchemy/databases/sqlite.py index 3c15d48b5f..49630b9ed3 100644 --- a/lib/sqlalchemy/databases/sqlite.py +++ b/lib/sqlalchemy/databases/sqlite.py @@ -150,7 +150,7 @@ class SQLiteSQLEngine(ansisql.ANSISQLEngine): (tablename, localcol, remotecol) = (row[2], row[3], row[4]) #print "row! " + repr(row) remotetable = Table(tablename, self, autoload = True) - table.c[localcol].foreign_key = schema.ForeignKey(remotetable.c[remotecol]) + table.c[localcol].append_item(schema.ForeignKey(remotetable.c[remotecol])) # check for UNIQUE indexes c = self.execute("PRAGMA index_list(" + table.name + ")", {}) unique_indexes = [] diff --git a/lib/sqlalchemy/schema.py b/lib/sqlalchemy/schema.py index 1d5b504e2e..026825737e 100644 --- a/lib/sqlalchemy/schema.py +++ b/lib/sqlalchemy/schema.py @@ -238,6 +238,9 @@ class Column(SchemaItem): original = property(lambda s: s._orig or s) engine = property(lambda s: s.table.engine) + def append_item(self, item): + self._init_items(item) + def _set_primary_key(self): if self.primary_key: return @@ -374,6 +377,10 @@ class ForeignKey(SchemaItem): def _set_parent(self, column): self.parent = column + # if a foreign key was already set up for this, replace it with + # this one, including removing from the parent + if self.parent.foreign_key is not None: + self.parent.table.foreign_keys.remove(self.parent.foreign_key) self.parent.foreign_key = self self.parent.table.foreign_keys.append(self) diff --git a/test/engines.py b/test/engines.py index 0ab3cb3e65..8a0bc24583 100644 --- a/test/engines.py +++ b/test/engines.py @@ -53,9 +53,16 @@ class EngineTest(PersistTest): users.create() addresses.create() - - addresses.drop() - users.drop() + try: + # create a join from the two tables, this insures that + # theres a foreign key set up +# addresses.c.remote_user_id.append_item(ForeignKey('engine_users.user_id')) + j = join(users, addresses) + print str(j.onclause) + self.assert_((users.c.user_id==addresses.c.remote_user_id).compare(j.onclause)) + finally: + addresses.drop() + users.drop() def testmultipk(self): table = Table( -- 2.47.2