On 09/02/2012 19:47, Michael Bayer wrote:
OK I'm not sure if you are on 0.7.5
Yep, this project is.
but the string name of the column is accepted for "polymorphic_on".
Cool, any plans for primary key?
The recipe just needs to fill in the 'inherits' keyword within prepare(), and
that's it. See below:
What I'm using is below:
class DeclarativeReflectedBase(object):
_mapper_args = []
@classmethod
def __mapper_cls__(cls, *args, **kw):
"""Declarative will use this function in lieu of
calling mapper() directly.
Collect each series of arguments and invoke
them when prepare() is called.
"""
cls._mapper_args.append((args, kw))
@classmethod
def prepare(cls, engine):
"""Reflect all the tables and map !"""
while cls._mapper_args:
args, kw = cls._mapper_args.pop(0)
klass = args[0]
# autoload Table, which is already
# present in the metadata. This
# will fill in db-loaded columns
# into the existing Table object.
if args[1] is not None:
table = args[1]
try:
Table(
table.name,
cls.metadata,
extend_existing=True,
autoload_replace=False,
autoload=True,
autoload_with=engine,
schema=table.schema
)
except NoSuchTableError:
continue
for c in klass.__bases__:
if _is_mapped_class(c):
kw['inherits'] = c
break
# deal with cases where we need to specify the primary key
# structure
primary_key_names = kw.pop('primary_key_names', None)
if primary_key_names:
kw['primary_key'] = pk = []
for name in primary_key_names:
pk.append(getattr(table, name))
klass.__mapper__ = mapper(*args, **kw)
# Use this one for concretely mapped models:
Base = declarative_base()
# The this one for reflected models:
class Reflected(DeclarativeReflectedBase, Base):
__abstract__ = True
Attempting to import the module containing the single table inheritance
models gives:
Traceback (most recent call last):
File "sqlalchemy/ext/declarative.py", line 1336, in __init__
_as_declarative(cls, classname, cls.__dict__)
File "sqlalchemy/ext/declarative.py", line 1235, in _as_declarative
**table_kw)
File "sqlalchemy/schema.py", line 290, in __new__
"existing Table object." % key)
sqlalchemy.exc.InvalidRequestError: Table 'the_table' is already defined
for this MetaData instance. Specify 'extend_existing=True' to redefine
options and columns on an existing Table object.
NB: This is before any call to prepare...
Chris
--
Simplistix - Content Management, Batch Processing & Python Consulting
- http://www.simplistix.co.uk
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.