Hi again,
I finally replaced my many-to-many relations with an association object :
(truncated for legibility)
languages = Table('languages', meta,
Column('id', Integer, primary_key=True),
Column('iso_code', String(2)),
Column('language', String(100)))
invasives = Table('invasives', meta,
Column('id', Integer, primary_key=True))
species_names = Table('species_names', meta,
Column('id', Integer, primary_key=True),
Column('name', String(200)))
invasive_names = Table('invasive_names', meta,
Column('language_id', Integer, ForeignKey('languages.id')),
Column('invasive_id', Integer, ForeignKey('invasives.id')),
Column('name_id', Integer, ForeignKey('species_names.id')))
#########
# MAPPERS #
#########
mapper(SpeciesName, species_names)
mapper(InvasiveName, invasive_names,
primary_key = [invasive_names.c.invasive_id,
invasive_names.c.name_id, invasive_names.c.language_id],
properties = {
'name' : relation(SpeciesName, lazy=True, cascade='all,
delete-orphan'),
'language' : relation(Language, lazy=True)
})
mapper(Invasive, invasives, properties = {
'names' : relation(InvasiveName, lazy=True, association=SpeciesName,
cascade='all, delete-orphan'),
'scientific_name' : relation(SpeciesName, secondary=invasive_names,
primaryjoin = invasive_names.c.invasive_id==invasives.c.id,
secondaryjoin =
and_(invasive_names.c.name_id==species_names.c.id,
invasive_names.c.language_id==languages.c.id,
languages.c.iso_code=='la'), uselist=False)
}
)
It works fine !, I can do things like:
invasive_object = Invasive()
invasive_name_object = InvasiveName()
language_object = query_language.get_by_iso_code('la')
species_name_object = SpeciesName()
species_name_object.name = 'blablabla'
invasive_name_object.name = species_name_object
invasive_name_object.language = language_object
invasive_object.names.append(invasive_name_object)
However, there is one thing that doesn't work : when I delete an
Invasive, the corresponding InvasiveName is deleted (association), but
_not_ the corresponding SpeciesName, even with cascade='all,
delete-orphan', is it normal ? Do I need to delete it manually with
session.delete(SpeciesName) ?
Thanks !
-------------------------------------------------------------------------
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