]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- On the same theme, the REFERENCES clause in a CREATE TABLE
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 12 Nov 2010 15:49:17 +0000 (10:49 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 12 Nov 2010 15:49:17 +0000 (10:49 -0500)
that includes a remote schema to a *different* schema
than that of the parent table doesn't render at all,
as cross-schema references do not appear to be supported.

CHANGES
lib/sqlalchemy/dialects/sqlite/base.py
test/dialect/test_sqlite.py

diff --git a/CHANGES b/CHANGES
index ee7fad4368bcb31765b42e2685c7aa3c5fd65090..7b8749a3f513a15ead97421b8009775dc17de820 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -38,9 +38,14 @@ CHANGES
 
 - sqlite
   - The REFERENCES clause in a CREATE TABLE that includes
-    a remote schema name now renders the remote name without
+    a remote schema to another table with the same schema 
+    name now renders the remote name without
     the schema clause, as required by SQLite.  [ticket:1851]
-    
+
+  - On the same theme, the REFERENCES clause in a CREATE TABLE
+    that includes a remote schema to a *different* schema
+    than that of the parent table doesn't render at all,
+    as cross-schema references do not appear to be supported.
     
 0.6.5
 =====
index 8ff93580b7ee7d2dad444187b8f4b4777aee5285..994904b6ac985827c8722d5aaf59054b8b4eb2eb 100644 (file)
@@ -270,7 +270,17 @@ class SQLiteDDLCompiler(compiler.DDLCompiler):
  
         return super(SQLiteDDLCompiler, self).\
                     visit_primary_key_constraint(constraint)
+                    
+    def visit_foreign_key_constraint(self, constraint):
+        
+        local_table = constraint._elements.values()[0].parent.table
+        remote_table = list(constraint._elements.values())[0].column.table
+        
+        if local_table.schema != remote_table.schema:
+            return None
+        else:
+            return super(SQLiteDDLCompiler, self).visit_foreign_key_constraint(constraint)
+
     def define_constraint_remote_table(self, constraint, table, preparer):
         """Format the remote table clause of a CREATE CONSTRAINT clause."""
         
index 9099ec5d70f1c429faaebe7e0512d2f127514720..19ec260d3790833dd8608770390c3bbb328aa6fc 100644 (file)
@@ -391,17 +391,47 @@ class SQLTest(TestBase, AssertsCompiledSQL):
                         schema='master')
         t2 = Table('t2', metadata, 
                         Column('id', Integer, primary_key=True),
-                        Column('t2_id', Integer, ForeignKey('master.t1.id')),
+                        Column('t1_id', Integer, ForeignKey('master.t1.id')),
                         schema='master'
                     )
+        t3 = Table('t3', metadata, 
+                        Column('id', Integer, primary_key=True),
+                        Column('t1_id', Integer, ForeignKey('master.t1.id')),
+                        schema='alternate'
+                    )
+        t4 = Table('t4', metadata, 
+                        Column('id', Integer, primary_key=True),
+                        Column('t1_id', Integer, ForeignKey('master.t1.id')),
+                    )
         
+        # schema->schema, generate REFERENCES with no schema name
         self.assert_compile(
             schema.CreateTable(t2),
                 "CREATE TABLE master.t2 ("
                 "id INTEGER NOT NULL, "
-                "t2_id INTEGER, "
+                "t1_id INTEGER, "
                 "PRIMARY KEY (id), "
-                "FOREIGN KEY(t2_id) REFERENCES t1 (id)"
+                "FOREIGN KEY(t1_id) REFERENCES t1 (id)"
+                ")"            
+        )
+
+        # schema->different schema, don't generate REFERENCES
+        self.assert_compile(
+            schema.CreateTable(t3),
+                "CREATE TABLE alternate.t3 ("
+                "id INTEGER NOT NULL, "
+                "t1_id INTEGER, "
+                "PRIMARY KEY (id)"
+                ")"            
+        )
+
+        # same for local schema
+        self.assert_compile(
+            schema.CreateTable(t4),
+                "CREATE TABLE t4 ("
+                "id INTEGER NOT NULL, "
+                "t1_id INTEGER, "
+                "PRIMARY KEY (id)"
                 ")"            
         )