summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGES6
-rw-r--r--lib/sqlalchemy/schema.py6
-rw-r--r--test/engine/test_metadata.py53
3 files changed, 61 insertions, 4 deletions
diff --git a/CHANGES b/CHANGES
index 23e16e2e9..ab0c14fff 100644
--- a/CHANGES
+++ b/CHANGES
@@ -181,6 +181,12 @@ CHANGES
Note that it is *not* built/installed by default.
See README for installation instructions.
+- metadata
+ - Added the ability to strip schema information when using
+ "tometadata" by passing "schema=None" as an argument. If schema
+ is not specified then the table's schema is retained.
+ [ticket: 1673]
+
- declarative
- DeclarativeMeta exclusively uses cls.__dict__ (not dict_)
as the source of class information; _as_declarative exclusively
diff --git a/lib/sqlalchemy/schema.py b/lib/sqlalchemy/schema.py
index 80c03bbba..3c6652807 100644
--- a/lib/sqlalchemy/schema.py
+++ b/lib/sqlalchemy/schema.py
@@ -43,6 +43,8 @@ __all__ = ['SchemaItem', 'Table', 'Column', 'ForeignKey', 'Sequence', 'Index',
]
__all__.sort()
+RETAIN_SCHEMA = util.symbol('retain_schema')
+
class SchemaItem(visitors.Visitable):
"""Base class for items that define a database schema."""
@@ -413,11 +415,11 @@ class Table(SchemaItem, expression.TableClause):
"""
self.metadata.drop_all(bind=bind, checkfirst=checkfirst, tables=[self])
- def tometadata(self, metadata, schema=None):
+ def tometadata(self, metadata, schema=RETAIN_SCHEMA):
"""Return a copy of this ``Table`` associated with a different ``MetaData``."""
try:
- if not schema:
+ if schema is RETAIN_SCHEMA:
schema = self.schema
key = _get_table_key(self.name, schema)
return metadata.tables[key]
diff --git a/test/engine/test_metadata.py b/test/engine/test_metadata.py
index 50109e15a..0d2cb7775 100644
--- a/test/engine/test_metadata.py
+++ b/test/engine/test_metadata.py
@@ -203,8 +203,57 @@ class MetaDataTest(TestBase, ComparesTables):
eq_(str(table_c.join(table2_c).onclause), str(table_c.c.myid == table2_c.c.myid))
eq_(str(table_c.join(table2_c).onclause), "someschema.mytable.myid = someschema.othertable.myid")
-
-
+
+ def test_tometadata_default_schema(self):
+ meta = MetaData()
+
+ table = Table('mytable', meta,
+ Column('myid', Integer, primary_key=True),
+ Column('name', String(40), nullable=True),
+ Column('description', String(30), CheckConstraint("description='hi'")),
+ UniqueConstraint('name'),
+ test_needs_fk=True,
+ schema='myschema',
+ )
+
+ table2 = Table('othertable', meta,
+ Column('id', Integer, primary_key=True),
+ Column('myid', Integer, ForeignKey('myschema.mytable.myid')),
+ test_needs_fk=True,
+ schema='myschema',
+ )
+
+ meta2 = MetaData()
+ table_c = table.tometadata(meta2)
+ table2_c = table2.tometadata(meta2)
+
+ eq_(str(table_c.join(table2_c).onclause), str(table_c.c.myid == table2_c.c.myid))
+ eq_(str(table_c.join(table2_c).onclause), "myschema.mytable.myid = myschema.othertable.myid")
+
+ def test_tometadata_strip_schema(self):
+ meta = MetaData()
+
+ table = Table('mytable', meta,
+ Column('myid', Integer, primary_key=True),
+ Column('name', String(40), nullable=True),
+ Column('description', String(30), CheckConstraint("description='hi'")),
+ UniqueConstraint('name'),
+ test_needs_fk=True,
+ )
+
+ table2 = Table('othertable', meta,
+ Column('id', Integer, primary_key=True),
+ Column('myid', Integer, ForeignKey('mytable.myid')),
+ test_needs_fk=True,
+ )
+
+ meta2 = MetaData()
+ table_c = table.tometadata(meta2, schema=None)
+ table2_c = table2.tometadata(meta2, schema=None)
+
+ eq_(str(table_c.join(table2_c).onclause), str(table_c.c.myid == table2_c.c.myid))
+ eq_(str(table_c.join(table2_c).onclause), "mytable.myid = othertable.myid")
+
def test_nonexistent(self):
assert_raises(tsa.exc.NoSuchTableError, Table,
'fake_table',