summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/sqlalchemy/dialects/mysql/base.py1
-rw-r--r--lib/sqlalchemy/schema.py20
-rw-r--r--lib/sqlalchemy/types.py51
3 files changed, 65 insertions, 7 deletions
diff --git a/lib/sqlalchemy/dialects/mysql/base.py b/lib/sqlalchemy/dialects/mysql/base.py
index 5f324e5dd..566a71da7 100644
--- a/lib/sqlalchemy/dialects/mysql/base.py
+++ b/lib/sqlalchemy/dialects/mysql/base.py
@@ -1061,6 +1061,7 @@ class ENUM(sqltypes.Enum, _StringType):
kw.pop('name', None)
kw.pop('quote', None)
kw.pop('native_enum', None)
+ kw.pop('inherit_schema', None)
_StringType.__init__(self, length=length, **kw)
sqltypes.Enum.__init__(self, *enums)
diff --git a/lib/sqlalchemy/schema.py b/lib/sqlalchemy/schema.py
index b9ee55abf..9d14bd3ca 100644
--- a/lib/sqlalchemy/schema.py
+++ b/lib/sqlalchemy/schema.py
@@ -634,16 +634,26 @@ class Table(SchemaItem, expression.TableClause):
E.g.::
+ some_engine = create_engine("sqlite:///some.db")
+
# create two metadata
- meta1 = MetaData('sqlite:///querytest.db')
+ meta1 = MetaData()
meta2 = MetaData()
# load 'users' from the sqlite engine
- users_table = Table('users', meta1, autoload=True)
+ users_table = Table('users', meta1, autoload=True,
+ autoload_with=some_engine)
# create the same Table object for the plain metadata
users_table_2 = users_table.tometadata(meta2)
+ :param metadata: Target :class:`.MetaData` object.
+ :param schema: Optional string name of a target schema, or
+ ``None`` for no schema. The :class:`.Table` object will be
+ given this schema name upon copy. Defaults to the special
+ symbol :attr:`.RETAIN_SCHEMA` which indicates no change should be
+ made to the schema name of the resulting :class:`.Table`.
+
"""
if schema is RETAIN_SCHEMA:
@@ -1094,9 +1104,13 @@ class Column(SchemaItem, expression.ColumnClause):
[c.copy(**kw) for c in self.constraints] + \
[c.copy(**kw) for c in self.foreign_keys if not c.constraint]
+ type_ = self.type
+ if isinstance(type_, sqltypes.SchemaType):
+ type_ = type_.copy(**kw)
+
c = self._constructor(
name=self.name,
- type_=self.type,
+ type_=type_,
key=self.key,
primary_key=self.primary_key,
nullable=self.nullable,
diff --git a/lib/sqlalchemy/types.py b/lib/sqlalchemy/types.py
index ce64bb83e..b9f7b9444 100644
--- a/lib/sqlalchemy/types.py
+++ b/lib/sqlalchemy/types.py
@@ -1791,6 +1791,13 @@ class SchemaType(events.SchemaEventTarget):
surrounding the association of the type object with a parent
:class:`.Column`.
+ .. seealso::
+
+ :class:`.Enum`
+
+ :class:`.Boolean`
+
+
"""
def __init__(self, **kw):
@@ -1798,6 +1805,7 @@ class SchemaType(events.SchemaEventTarget):
self.quote = kw.pop('quote', None)
self.schema = kw.pop('schema', None)
self.metadata = kw.pop('metadata', None)
+ self.inherit_schema = kw.pop('inherit_schema', False)
if self.metadata:
event.listen(
self.metadata,
@@ -1814,6 +1822,9 @@ class SchemaType(events.SchemaEventTarget):
column._on_table_attach(util.portable_instancemethod(self._set_table))
def _set_table(self, column, table):
+ if self.inherit_schema:
+ self.schema = table.schema
+
event.listen(
table,
"before_create",
@@ -1839,6 +1850,20 @@ class SchemaType(events.SchemaEventTarget):
util.portable_instancemethod(self._on_metadata_drop)
)
+ def copy(self, **kw):
+ return self.adapt(self.__class__)
+
+ def adapt(self, impltype, **kw):
+ schema = kw.pop('schema', self.schema)
+ metadata = kw.pop('metadata', self.metadata)
+ return impltype(name=self.name,
+ quote=self.quote,
+ schema=schema,
+ metadata=metadata,
+ inherit_schema=self.inherit_schema,
+ **kw
+ )
+
@property
def bind(self):
return self.metadata and self.metadata.bind or None
@@ -1891,7 +1916,7 @@ class Enum(String, SchemaType):
By default, uses the backend's native ENUM type if available,
else uses VARCHAR + a CHECK constraint.
- See also:
+ .. seealso::
:class:`~.postgresql.ENUM` - PostgreSQL-specific type,
which has additional functionality.
@@ -1933,16 +1958,31 @@ class Enum(String, SchemaType):
available. Defaults to True. When False, uses VARCHAR + check
constraint for all backends.
- :param schema: Schemaname of this type. For types that exist on the
+ :param schema: Schema name of this type. For types that exist on the
target database as an independent schema construct (Postgresql),
this parameter specifies the named schema in which the type is
present.
+ .. note::
+
+ The ``schema`` of the :class:`.Enum` type does not
+ by default make use of the ``schema`` established on the
+ owning :class:`.Table`. If this behavior is desired,
+ set the ``inherit_schema`` flag to ``True``.
+
:param quote: Force quoting to be on or off on the type's name. If
left as the default of `None`, the usual schema-level "case
sensitive"/"reserved name" rules are used to determine if this
type's name should be quoted.
+ :param inherit_schema: When ``True``, the "schema" from the owning
+ :class:`.Table` will be copied to the "schema" attribute of this
+ :class:`.Enum`, replacing whatever value was passed for the
+ ``schema`` attribute. This also takes effect when using the
+ :meth:`.Table.tometadata` operation.
+
+ .. versionadded:: 0.8
+
"""
self.enums = enums
self.native_enum = kw.pop('native_enum', True)
@@ -1988,13 +2028,16 @@ class Enum(String, SchemaType):
table.append_constraint(e)
def adapt(self, impltype, **kw):
+ schema = kw.pop('schema', self.schema)
+ metadata = kw.pop('metadata', self.metadata)
if issubclass(impltype, Enum):
return impltype(name=self.name,
quote=self.quote,
- schema=self.schema,
- metadata=self.metadata,
+ schema=schema,
+ metadata=metadata,
convert_unicode=self.convert_unicode,
native_enum=self.native_enum,
+ inherit_schema=self.inherit_schema,
*self.enums,
**kw
)