I'm not certain if I'm doing something wrong here, or if this is a
bug. There's a test module at the end of this message.
I've got a table that has two separate columns pointing back to
another table. I've set up relations for both of those and I used
primaryjoin to specify which field is used for each relation (that's
where I might be going wrong). The first suspicious thing I see in the
log is an INSERT into this table where both values are the same:
[2006-07-26 11:23:01,523] [engine]: INSERT INTO lunch (want_id,
dontwant_id) VALUES (?, ?)
[2006-07-26 11:23:01,523] [engine]: [7, 7]
and the next suspicious thing is a query at the end that is guaranteed
to not produce results:
[2006-07-26 11:23:01,531] [engine]: SELECT foods.id AS foods_id,
foods.name AS foods_name
FROM foods
WHERE foods.id = ? AND foods.id = ? ORDER BY foods.oid
[2006-07-26 11:23:01,531] [engine]: [2, 1]
I think this scenario *should* work, but I could certainly be doing
something wrong.
Thanks!
Kevin
from sqlalchemy import *
engine = create_engine("sqlite://memory", echo=True)
metadata = BoundMetaData(engine)
session = create_session(bind_to=engine)
foods_table = Table("foods", metadata,
Column("id", Integer, primary_key=True),
Column("name", String)
)
lunch_table = Table("lunch", metadata,
Column("want_id", Integer, ForeignKey("foods.id")),
Column("id", Integer, primary_key=True),
Column("dontwant_id", Integer, ForeignKey("foods.id"))
)
class Food(object):
pass
class Lunch(object):
pass
food_mapper = mapper(Food, foods_table, properties=dict(
wanted = relation(Lunch,
primaryjoin=lunch_table.c.want_id==foods_table.c.id,
backref="want"),
notwanted = relation(Lunch,
primaryjoin=lunch_table.c.dontwant_id==foods_table.c.id,
backref="dontwant")
)
)
lunch_mapper = mapper(Lunch, lunch_table)
def test_food():
metadata.create_all()
burger = Food()
burger.name="Burger"
session.save(burger)
dosa = Food()
dosa.name = "Masala Dosa"
session.save(dosa)
lunch = Lunch()
lunch.want = dosa
lunch.dontwant = burger
session.save(lunch)
session.flush()
lunch = session.get(Lunch, 1)
print lunch.want.name
assert lunch.want.name == "Masala Dosa"
print lunch.dontwant.name
assert lunch.dontwant.name == "Burger"
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Sqlalchemy-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users