ok, i committed a very small change and applied several layers of scotch tape to your example and it is roughly working. attached is a test script which documents a few of the quirks with this sort of thing right now. I have in mind some refactorings to make mapper inheritance a little stronger, after working with this example.

from sqlalchemy import *
import sqlalchemy

engine = create_engine('sqlite://', echo=True)

foo = Table('foo', engine,
    Column('id', Integer, primary_key=True),
	Column('data', String(20)),
	).create()

bar = Table('bar', engine,
    Column('id', Integer, ForeignKey('foo.id'), primary_key=True),
	).create()

foo_bar = Table('foo_bar', engine,
    Column('foo_id', Integer, ForeignKey('foo.id')),
    Column('bar_id', Integer, ForeignKey('bar.id'))).create()

class Foo(object): 
	def __init__(self, data=None):
		self.data = data
	def __str__(self):
		return "Foo(%s)" % self.data
	def __repr__(self):
		return str(self)

Foo.mapper = mapper(Foo, foo)
class Bar(Foo):
	def __str__(self):
		return "Bar(%s)" % self.data

Bar.mapper = mapper(Bar, bar, inherits=Foo.mapper, properties = {
		# this is needed at the moment if the join 
		# columns in 'foo' and 'bar' are not the same name
		#'id':[bar.c.bid, foo.c.id]
	})

# the join conditions come out nicer if you specify them explicitly
Bar.mapper.add_property('foos', relation(Foo.mapper, foo_bar, primaryjoin=bar.c.id==foo_bar.c.bar_id, secondaryjoin=foo_bar.c.foo_id==foo.c.id, lazy=False))

# but this works too
#Bar.mapper.add_property('foos', relation(Foo.mapper, foo_bar, lazy=False))


b = Bar('barfoo')
objectstore.commit()

b.foos.append(Foo('subfoo1'))
b.foos.append(Foo('subfoo2'))

objectstore.commit()
objectstore.clear()

l =b.mapper.select()
print l[0]
print l[0].foos



On Feb 25, 2006, at 6:37 PM, Florian Boesch wrote:

I require that kind of thing, so I've assembled a little testcode to demonstrate
the problems.

Below is the vanilla many to many thing that doesn't error upon defintion (with
one minor glitch at the mapper, see #comments)

from sqlalchemy import *
from sqlalchemy.ext.proxy import ProxyEngine
engine = ProxyEngine()

foo = Table('foo', engine,
    Column('id', Integer, primary_key=True))

bar = Table('bar', engine,
    Column('id', Integer, primary_key=True))

foo_bar = Table('foo_bar', engine,
    Column('foo_id', Integer, ForeignKey('foo.id')),
    Column('bar_id', Integer, ForeignKey('bar.id')))

class Foo(object): pass
Foo.mapper = mapper(Foo, foo)
class Bar(object): pass
Bar.mapper = mapper(Bar, bar)

# Bar.mapper.add_property('foos', relation(Foo, foo_bar))
# ==> sqlalchemy.exceptions.ArgumentError: relation(class, table, **kwargs) is # deprecated. Please use relation(class, **kwargs) or relation (mapper,
**kwargs).

Bar.mapper.add_property('foos', relation(Foo.mapper, foo_bar))


Now when I throw in inheritance as in

bar = Table('bar', engine,
    Column('id', Integer, ForeignKey('foo.id'), primary_key=True))
Bar.mapper = mapper(Bar, bar, inherits=Foo.mapper)

and then try to
Bar.mapper.add_property('foos', relation(Foo.mapper, foo_bar))

I get:
Traceback (most recent call last):
  File "testalchemy.py", line 23, in ?
    Bar.mapper.add_property('foos', relation(Foo.mapper, foo_bar))
  File
"/usr/local/lib/python2.4/site-packages/SQLAlchemy-0.1.2-py2.4.egg/ sqlalchemy/mapping/mapper.py",
line 195, in add_property
    prop.init(key, self)
  File
"/usr/local/lib/python2.4/site-packages/SQLAlchemy-0.1.2-py2.4.egg/ sqlalchemy/mapping/mapper.py",
line 777, in init
    self.do_init(key, parent)
  File
"/usr/local/lib/python2.4/site-packages/SQLAlchemy-0.1.2-py2.4.egg/ sqlalchemy/mapping/properties.py",
line 191, in do_init
    self._compile_synchronizers()
  File
"/usr/local/lib/python2.4/site-packages/SQLAlchemy-0.1.2-py2.4.egg/ sqlalchemy/mapping/properties.py",
line 583, in _compile_synchronizers
    self.primaryjoin.accept_visitor(processor)
  File
"/usr/local/lib/python2.4/site-packages/SQLAlchemy-0.1.2-py2.4.egg/ sqlalchemy/sql.py",
line 749, in accept_visitor
    c.accept_visitor(visitor)
  File
"/usr/local/lib/python2.4/site-packages/SQLAlchemy-0.1.2-py2.4.egg/ sqlalchemy/sql.py",
line 819, in accept_visitor
    visitor.visit_binary(self)
  File
"/usr/local/lib/python2.4/site-packages/SQLAlchemy-0.1.2-py2.4.egg/ sqlalchemy/mapping/properties.py",
line 969, in visit_binary
    self.func(binary)
  File
"/usr/local/lib/python2.4/site-packages/SQLAlchemy-0.1.2-py2.4.egg/ sqlalchemy/mapping/properties.py",
line 575, in compile
    raise AssertionError("assert failed")
sqlalchemy.exceptions.AssertionError: assert failed

(a sidenote, assert failed isn't a very clever assert line :D)

----- End forwarded message -----




-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language that extends applications into web and mobile media. Attend the live webcast and join the prime developer group breaking into this new coding territory! http://sel.as-us.falkag.net/sel? cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
Sqlalchemy-users mailing list
Sqlalchemy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to