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 /lib | |
| 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 'lib')
| -rw-r--r-- | lib/sqlalchemy/__init__.py | 1 | ||||
| -rw-r--r-- | lib/sqlalchemy/engine/reflection.py | 1 | ||||
| -rw-r--r-- | lib/sqlalchemy/schema.py | 1 | ||||
| -rw-r--r-- | lib/sqlalchemy/sql/schema.py | 52 |
4 files changed, 51 insertions, 4 deletions
diff --git a/lib/sqlalchemy/__init__.py b/lib/sqlalchemy/__init__.py index 1193a1b0b..b1d240edf 100644 --- a/lib/sqlalchemy/__init__.py +++ b/lib/sqlalchemy/__init__.py @@ -120,6 +120,7 @@ from .schema import ( ThreadLocalMetaData, UniqueConstraint, DDL, + BLANK_SCHEMA ) diff --git a/lib/sqlalchemy/engine/reflection.py b/lib/sqlalchemy/engine/reflection.py index eaa5e2e48..2d524978d 100644 --- a/lib/sqlalchemy/engine/reflection.py +++ b/lib/sqlalchemy/engine/reflection.py @@ -693,6 +693,7 @@ class Inspector(object): else: sa_schema.Table(referred_table, table.metadata, autoload=True, autoload_with=self.bind, + schema=sa_schema.BLANK_SCHEMA, **reflection_options ) for column in referred_columns: diff --git a/lib/sqlalchemy/schema.py b/lib/sqlalchemy/schema.py index 5b703f7b6..bd0cbe54e 100644 --- a/lib/sqlalchemy/schema.py +++ b/lib/sqlalchemy/schema.py @@ -15,6 +15,7 @@ from .sql.base import ( from .sql.schema import ( + BLANK_SCHEMA, CheckConstraint, Column, ColumnDefault, diff --git a/lib/sqlalchemy/sql/schema.py b/lib/sqlalchemy/sql/schema.py index 5e709b1e3..64692644c 100644 --- a/lib/sqlalchemy/sql/schema.py +++ b/lib/sqlalchemy/sql/schema.py @@ -46,6 +46,17 @@ from . import ddl RETAIN_SCHEMA = util.symbol('retain_schema') +BLANK_SCHEMA = util.symbol( + 'blank_schema', + """Symbol indicating that a :class:`.Table` or :class:`.Sequence` + should have 'None' for its schema, even if the parent + :class:`.MetaData` has specified a schema. + + .. versionadded:: 1.0.14 + + """ +) + def _get_table_key(name, schema): if schema is None: @@ -340,6 +351,17 @@ class Table(DialectKWArgs, SchemaItem, TableClause): the table resides in a schema other than the default selected schema for the engine's database connection. Defaults to ``None``. + If the owning :class:`.MetaData` of this :class:`.Table` specifies + its own :paramref:`.MetaData.schema` parameter, then that schema + name will be applied to this :class:`.Table` if the schema parameter + here is set to ``None``. To set a blank schema name on a :class:`.Table` + that would otherwise use the schema set on the owning :class:`.MetaData`, + specify the special symbol :attr:`.BLANK_SCHEMA`. + + .. versionadded:: 1.0.14 Added the :attr:`.BLANK_SCHEMA` symbol to + allow a :class:`.Table` to have a blank schema name even when the + parent :class:`.MetaData` specifies :paramref:`.MetaData.schema`. + The quoting rules for the schema name are the same as those for the ``name`` parameter, in that quoting is applied for reserved words or case-sensitive names; to enable unconditional quoting for the @@ -371,6 +393,8 @@ class Table(DialectKWArgs, SchemaItem, TableClause): schema = kw.get('schema', None) if schema is None: schema = metadata.schema + elif schema is BLANK_SCHEMA: + schema = None keep_existing = kw.pop('keep_existing', False) extend_existing = kw.pop('extend_existing', False) if 'useexisting' in kw: @@ -442,6 +466,8 @@ class Table(DialectKWArgs, SchemaItem, TableClause): self.schema = kwargs.pop('schema', None) if self.schema is None: self.schema = metadata.schema + elif self.schema is BLANK_SCHEMA: + self.schema = None else: quote_schema = kwargs.pop('quote_schema', None) self.schema = quoted_name(self.schema, quote_schema) @@ -2120,7 +2146,10 @@ class Sequence(DefaultGenerator): .. versionadded:: 1.0.7 :param schema: Optional schema name for the sequence, if located - in a schema other than the default. + in a schema other than the default. The rules for selecting the + schema name when a :class:`.MetaData` is also present are the same + as that of :paramref:`.Table.schema`. + :param optional: boolean value, when ``True``, indicates that this :class:`.Sequence` object only needs to be explicitly generated on backends that don't provide another way to generate primary @@ -2169,7 +2198,9 @@ class Sequence(DefaultGenerator): self.nomaxvalue = nomaxvalue self.cycle = cycle self.optional = optional - if metadata is not None and schema is None and metadata.schema: + if schema is BLANK_SCHEMA: + self.schema = schema = None + elif metadata is not None and schema is None and metadata.schema: self.schema = schema = metadata.schema else: self.schema = quoted_name(schema, quote_schema) @@ -3372,8 +3403,21 @@ class MetaData(SchemaItem): :param schema: The default schema to use for the :class:`.Table`, - :class:`.Sequence`, and other objects associated with this - :class:`.MetaData`. Defaults to ``None``. + :class:`.Sequence`, and potentially other objects associated with + this :class:`.MetaData`. Defaults to ``None``. + + When this value is set, any :class:`.Table` or :class:`.Sequence` + which specifies ``None`` for the schema parameter will instead + have this schema name defined. To build a :class:`.Table` + or :class:`.Sequence` that still has ``None`` for the schema + even when this parameter is present, use the :attr:`.BLANK_SCHEMA` + symbol. + + .. seealso:: + + :paramref:`.Table.schema` + + :paramref:`.Sequence.schema` :param quote_schema: Sets the ``quote_schema`` flag for those :class:`.Table`, |
