Hello,
I'm trying to create an association between two objects of the same
type. For example I have table A and then I have an association table
that has two foreign keys to table A.
What I'm looking for is to be able to say:
one_typeA.append(two_typeA)
but the association_proxy seems only to be able to set the associated
column and doesn't fill in the both foreign keys based on the relation.
sqlalchemy.exceptions.IntegrityError: (IntegrityError)
species_synonym.synonym_id may not be NULL u'INSERT INTO species_synonym
(species_id, synonym_id) VALUES (?, ?)' [1, None]
I've also tried without using the association_proxy but I get an error
telling me that the collection I'm appending to expects the type of the
association table and not the type of the table I'm trying to associate.
sqlalchemy.exceptions.FlushError: Attempting to flush an item of type
<class '__main__.Species'> on collection 'Species.synonyms
(SpeciesSynonym)', which is handled by mapper 'Mapper|SpeciesSynonym|
species_synonym' and does not load items of that type. Did you mean to
use a polymorphic mapper for this relationship ? Set
'enable_typechecks=False' on the relation() to disable this exception.
Mismatched typeloading may cause bi-directional relationships (backrefs)
to not function properly.
Has anyone tried this or gotten it to work?
See the attachment for the code.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.associationproxy import association_proxy
uri = 'sqlite:///:memory:'
metadata = MetaData()
species_table = Table('species', metadata,
Column('id', Integer, primary_key=True),
Column('sp', String(64)))
species_synonym_table = Table('species_synonym', metadata,
Column('id', Integer, primary_key=True),
Column('species_id', Integer, ForeignKey('species.id'),
nullable=False),
Column('synonym_id', Integer, ForeignKey('species.id'),
nullable=False))
class Species(object):
synonyms = association_proxy('_synonyms', 'synonym')
pass
class SpeciesSynonym(object):
pass
mapper(Species, species_table,
properties = \
{'_synonyms':
relation(SpeciesSynonym,
primaryjoin=species_table.c.id==species_synonym_table.c.species_id,
cascade='all, delete-orphan', uselist=True
)})
mapper(SpeciesSynonym, species_synonym_table,
properties = \
{'synonym':
relation(Species, uselist=False,
primaryjoin=species_synonym_table.c.synonym_id==species_table.c.id),
'species':
relation(Species, uselist=False,
primaryjoin=species_synonym_table.c.species_id==species_table.c.id)})
engine = create_engine(uri)
engine.connect()
metadata.bind = engine
metadata.create_all()
session = create_session()
species_table.insert().execute({'id': 1, 'sp': 'test species 1'})
species_table.insert().execute({'id': 2, 'sp': 'test species 2'})
s = session.load(Species, 1)
sp2 = session.load(Species, 2)
s.synonyms.append(sp2)
session.flush()