Jonathan LaCour wrote:
> My fault. You need to use __metadata__, not __engine__, now that we are
> in the 0.2 series. Let me know if that works for you!
Works great for that example. I'm not sure, though, how to specify
metadata other than activemapper.metadata (which I'm having some issues
with due to its thread-local characteristics -- discussed in another
post) when trying to do many-to-many joins. See this example:
======================================================================
import sqlalchemy.mods.threadlocal
from sqlalchemy import *
from sqlalchemy.ext.activemapper import *
import sqlalchemy.ext.activemapper as activemapper
__engine__ = create_engine('sqlite:///:memory:')
meta = BoundMetaData(__engine__)
foo_bar_map_table = Table('foo_bar_map', meta,
Column('foo_id', Integer, ForeignKey('foo_tbl.foo_id'),
primary_key=True),
Column('bar_id', Integer, ForeignKey('bar_tbl.bar_id'),
primary_key=True))
class Foo(ActiveMapper):
class mapping:
__table__ = 'foo_tbl'
__meta__ = meta
foo_id = column(Integer, primary_key=True)
foo_text = column(String(30))
bars = many_to_many('Bar', foo_bar_map_table, backref='foos')
class Bar(ActiveMapper):
class mapping:
__table__ = 'bar_tbl'
__meta__ = meta
bar_id = column(Integer, primary_key=True)
bar_text = column(String(30))
#foos = many_to_many('Foo', foo_bar_map_table, backref='bars')
Foo.table.create()
foo_1 = Foo(foo_id=1, foo_text='qux')
bar_2 = Bar(bar_id=2, bar_ext='quux')
======================================================================
Traceback (most recent call last):
File "test3.py", line 22, in ?
class Bar(ActiveMapper):
File
"/home/ccd/VC/svn/sqlalchemy/lib/sqlalchemy/ext/activemapper.py", line
211, in __init__
process_relationships(cls)
File
"/home/ccd/VC/svn/sqlalchemy/lib/sqlalchemy/ext/activemapper.py", line
151, in process_relationships
process_relationships(deferred_class, was_deferred=True)
File
"/home/ccd/VC/svn/sqlalchemy/lib/sqlalchemy/ext/activemapper.py", line
135, in process_relationships
class_mapper(klass).add_properties(relations)
File "/home/ccd/VC/svn/sqlalchemy/lib/sqlalchemy/orm/mapper.py", line
501, in add_properties
self.add_property(key, value)
File "/home/ccd/VC/svn/sqlalchemy/lib/sqlalchemy/orm/mapper.py", line
509, in add_property
self._compile_property(key, prop, init=True)
File "/home/ccd/VC/svn/sqlalchemy/lib/sqlalchemy/orm/mapper.py", line
556, in _compile_property
prop.init(key, self)
File "/home/ccd/VC/svn/sqlalchemy/lib/sqlalchemy/orm/mapper.py", line
1120, in init
self.do_init(key, parent)
File "/home/ccd/VC/svn/sqlalchemy/lib/sqlalchemy/orm/properties.py",
line 209, in do_init
self.secondaryjoin = sql.join(self.mapper.unjoined_table,
self.secondary).onclause
File "/home/ccd/VC/svn/sqlalchemy/lib/sqlalchemy/sql.py", line 39, in
join
return Join(left, right, onclause, **kwargs)
File "/home/ccd/VC/svn/sqlalchemy/lib/sqlalchemy/sql.py", line 1047,
in __init__
self.onclause = self._match_primaries(left, right)
File "/home/ccd/VC/svn/sqlalchemy/lib/sqlalchemy/sql.py", line 1068,
in _match_primaries
if fk.references(primary):
File "/home/ccd/VC/svn/sqlalchemy/lib/sqlalchemy/schema.py", line
433, in references
return table.corresponding_column(self.column, False) is not None
File "/home/ccd/VC/svn/sqlalchemy/lib/sqlalchemy/schema.py", line
467, in <lambda>
column = property(lambda s: s._init_column())
File "/home/ccd/VC/svn/sqlalchemy/lib/sqlalchemy/schema.py", line
457, in _init_column
table = Table(tname, parenttable.metadata, mustexist=True,
schema=schema)
File "/home/ccd/VC/svn/sqlalchemy/lib/sqlalchemy/schema.py", line 86,
in __call__
raise exceptions.ArgumentError("Table '%s.%s' not defined" %
(schema, name))
sqlalchemy.exceptions.ArgumentError: Table 'None.foo_tbl' not defined
======================================================================
It would appear that there's a bit more to specifying the metadata
correctly than just using the same metadata for the ActiveMapper classes
and the join tables. Would you mind providing some enlightenment?
_______________________________________________
Sqlalchemy-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users