]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- The REFERENCES clause in a CREATE TABLE that includes
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 12 Nov 2010 15:36:03 +0000 (10:36 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 12 Nov 2010 15:36:03 +0000 (10:36 -0500)
a remote schema name now renders the remote name without
the schema clause, as required by SQLite.  [ticket:1851]

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

diff --git a/CHANGES b/CHANGES
index 7a07b7c7660451e243f8c5aff4023f7bd121757e..ee7fad4368bcb31765b42e2685c7aa3c5fd65090 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -35,6 +35,12 @@ CHANGES
     has_table() property works again.  Regression from
     0.6.3 (we don't have a Jython buildbot, sorry)
     [ticket:1960]
+
+- sqlite
+  - The REFERENCES clause in a CREATE TABLE that includes
+    a remote schema name now renders the remote name without
+    the schema clause, as required by SQLite.  [ticket:1851]
+    
     
 0.6.5
 =====
index b84b18e68e01326dbe8a311fc86737b958455f9a..8ff93580b7ee7d2dad444187b8f4b4777aee5285 100644 (file)
@@ -271,6 +271,10 @@ class SQLiteDDLCompiler(compiler.DDLCompiler):
         return super(SQLiteDDLCompiler, self).\
                     visit_primary_key_constraint(constraint)
  
+    def define_constraint_remote_table(self, constraint, table, preparer):
+        """Format the remote table clause of a CREATE CONSTRAINT clause."""
+        
+        return preparer.format_table(table, use_schema=False)
 
     def visit_create_index(self, create):
         index = create.element
index d3b8bf023ab195c2af18c92646f0b8a8fe78a75d..4b41c6ed33269dcd8197fea8cce124cc64c85905 100644 (file)
@@ -1306,7 +1306,7 @@ class DDLCompiler(engine.Compiled):
         text += "FOREIGN KEY(%s) REFERENCES %s (%s)" % (
             ', '.join(preparer.quote(f.parent.name, f.parent.quote)
                       for f in constraint._elements.values()),
-            preparer.format_table(remote_table),
+            self.define_constraint_remote_table(constraint, remote_table, preparer),
             ', '.join(preparer.quote(f.column.name, f.column.quote)
                       for f in constraint._elements.values())
         )
@@ -1314,6 +1314,11 @@ class DDLCompiler(engine.Compiled):
         text += self.define_constraint_deferrability(constraint)
         return text
 
+    def define_constraint_remote_table(self, constraint, table, preparer):
+        """Format the remote table clause of a CREATE CONSTRAINT clause."""
+        
+        return preparer.format_table(table)
+
     def visit_unique_constraint(self, constraint):
         text = ""
         if constraint.name is not None:
index 0cdd3848e26f7eb7c6f4fe5a00d7810719147da3..9099ec5d70f1c429faaebe7e0512d2f127514720 100644 (file)
@@ -384,6 +384,27 @@ class SQLTest(TestBase, AssertsCompiledSQL):
                                 "SELECT CAST(STRFTIME('%s', t.col1) AS "
                                 "INTEGER) AS anon_1 FROM t" % subst)
 
+    def test_constraints_with_schemas(self):
+        metadata = MetaData()
+        t1 = Table('t1', metadata, 
+                        Column('id', Integer, primary_key=True),
+                        schema='master')
+        t2 = Table('t2', metadata, 
+                        Column('id', Integer, primary_key=True),
+                        Column('t2_id', Integer, ForeignKey('master.t1.id')),
+                        schema='master'
+                    )
+        
+        self.assert_compile(
+            schema.CreateTable(t2),
+                "CREATE TABLE master.t2 ("
+                "id INTEGER NOT NULL, "
+                "t2_id INTEGER, "
+                "PRIMARY KEY (id), "
+                "FOREIGN KEY(t2_id) REFERENCES t1 (id)"
+                ")"            
+        )
+
 
 class InsertTest(TestBase, AssertsExecutionResults):