diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2016-05-18 11:07:02 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2016-05-18 11:43:38 -0400 |
| commit | c124fa36d5af2c85c87c51d24e92387adffbe3d2 (patch) | |
| tree | e065a4fedb2f79a0d7f5267a91fa5e789befeb8b /test | |
| parent | fb45433f1504978c63a1318a0dc616d79cfff04d (diff) | |
| download | sqlalchemy-c124fa36d5af2c85c87c51d24e92387adffbe3d2.tar.gz | |
Support "blank" schema when MetaData.schema is set
Previously, it was impossible to have a Table that has
None for a schema name when the "schema" parameter on
MetaData was set. A new symbol sqlalchemy.schema.BLANK_SCHEMA
is added which indicates that the schema name should unconditionally
be set to None. In particular, this value must be passed within
cross-schema foreign key reflection, so that a Table which
is in the "default" schema can be represented properly.
Fixes: #3716
Change-Id: I3d24f99c22cded206c5379fd32a225e74edb7a8e
Diffstat (limited to 'test')
| -rw-r--r-- | test/dialect/postgresql/test_reflection.py | 23 | ||||
| -rw-r--r-- | test/engine/test_reflection.py | 25 | ||||
| -rw-r--r-- | test/sql/test_metadata.py | 5 |
3 files changed, 52 insertions, 1 deletions
diff --git a/test/dialect/postgresql/test_reflection.py b/test/dialect/postgresql/test_reflection.py index 8da18108f..4897c4a7e 100644 --- a/test/dialect/postgresql/test_reflection.py +++ b/test/dialect/postgresql/test_reflection.py @@ -582,6 +582,29 @@ class ReflectionTest(fixtures.TestBase): ['test_schema_2.some_other_table', 'test_schema.some_table'])) @testing.provide_metadata + def test_cross_schema_reflection_metadata_uses_schema(self): + # test [ticket:3716] + + metadata = self.metadata + + Table('some_table', metadata, + Column('id', Integer, primary_key=True), + Column('sid', Integer, ForeignKey('some_other_table.id')), + schema='test_schema' + ) + Table('some_other_table', metadata, + Column('id', Integer, primary_key=True), + schema=None + ) + metadata.create_all() + with testing.db.connect() as conn: + meta2 = MetaData(conn, schema="test_schema") + meta2.reflect() + + eq_(set(meta2.tables), set( + ['some_other_table', 'test_schema.some_table'])) + + @testing.provide_metadata def test_uppercase_lowercase_table(self): metadata = self.metadata diff --git a/test/engine/test_reflection.py b/test/engine/test_reflection.py index f9799fda0..0d98147cb 100644 --- a/test/engine/test_reflection.py +++ b/test/engine/test_reflection.py @@ -1304,6 +1304,31 @@ class SchemaTest(fixtures.TestBase): 'sa_fake_schema_123'), False) @testing.requires.schemas + @testing.requires.cross_schema_fk_reflection + @testing.provide_metadata + def test_blank_schema_arg(self): + metadata = self.metadata + + Table('some_table', metadata, + Column('id', Integer, primary_key=True), + Column('sid', Integer, sa.ForeignKey('some_other_table.id')), + schema=testing.config.test_schema + ) + Table('some_other_table', metadata, + Column('id', Integer, primary_key=True), + schema=None + ) + metadata.create_all() + with testing.db.connect() as conn: + meta2 = MetaData(conn, schema=testing.config.test_schema) + meta2.reflect() + + eq_(set(meta2.tables), set( + [ + 'some_other_table', + '%s.some_table' % testing.config.test_schema])) + + @testing.requires.schemas @testing.fails_on('sqlite', 'FIXME: unknown') @testing.fails_on('sybase', 'FIXME: unknown') def test_explicit_default_schema(self): diff --git a/test/sql/test_metadata.py b/test/sql/test_metadata.py index 050929d3d..449956fcd 100644 --- a/test/sql/test_metadata.py +++ b/test/sql/test_metadata.py @@ -7,7 +7,8 @@ from sqlalchemy import Integer, String, UniqueConstraint, \ CheckConstraint, ForeignKey, MetaData, Sequence, \ ForeignKeyConstraint, PrimaryKeyConstraint, ColumnDefault, Index, event,\ events, Unicode, types as sqltypes, bindparam, \ - Table, Column, Boolean, Enum, func, text, TypeDecorator + Table, Column, Boolean, Enum, func, text, TypeDecorator, \ + BLANK_SCHEMA from sqlalchemy import schema, exc from sqlalchemy.engine import default from sqlalchemy.sql import elements, naming @@ -446,6 +447,7 @@ class MetaDataTest(fixtures.TestBase, ComparesTables): ('t2', m1, 'sch2', None, 'sch2', None), ('t3', m1, 'sch2', True, 'sch2', True), ('t4', m1, 'sch1', None, 'sch1', None), + ('t5', m1, BLANK_SCHEMA, None, None, None), ('t1', m2, None, None, 'sch1', True), ('t2', m2, 'sch2', None, 'sch2', None), ('t3', m2, 'sch2', True, 'sch2', True), @@ -458,6 +460,7 @@ class MetaDataTest(fixtures.TestBase, ComparesTables): ('t2', m4, 'sch2', None, 'sch2', None), ('t3', m4, 'sch2', True, 'sch2', True), ('t4', m4, 'sch1', None, 'sch1', None), + ('t5', m4, BLANK_SCHEMA, None, None, None), ]): kw = {} if schema is not None: |
