Hello everyone,

First of all, Michael, BIG "thank you" for an amazing piece of
software!

I am relatively new to SQLAlchemy (so please forgive any n00b
stupidity) -- but I use it for about two months with no issue at all
(until now). My problem is: I have a classic one-to-many relationship:
CbRecord being the parent and having many CbPersons. The CbPerson
primary key consists of two fields -- 'permalink' and 'domain', the
second being the foreign key of the above-mentioned relationship at
the same time. The relevant code follows:


Base = declarative_base()


class CbPerson(Base):

    __tablename__ = 'cb_persons'

    permalink           = Column(String(48), primary_key=True)
    domain              = Column(String(32), ForeignKey
('cb_record.domain'), primary_key=True)

    title               = Column(String(128))
    first_name          = Column(String(32))
    last_name           = Column(String(32))


class CbRecord(Base, Cacheable):
    __tablename__ = 'cb_record'

    domain              = Column(String(32), primary_key=True)
    permalink           = Column(String(48), nullable=False)
    name                = Column(String(16))

    # 'delete-orphan' is important as nullifying the foreign key is
impossible -- it is part of the PK
    persons = relation(CbPerson, backref='parent', cascade="all,
delete, delete-orphan")



Now when the database is empty, my test case can create these objects
with no issue. However, when running my tests a second time, I get
this error:

"FlushError: Instance <CbPerson at 0xXXXXXXX> is an unsaved, pending
instance and is an orphan (is not attached to any parent 'CbRecord'
instance via that classes' 'persons' attribute)"

The method the error is raised by is:

    def _storeProfileToCache(self, profile):
        # this creates the object in memory, out of a dictionary:
        newRecord = CbRecord.createFromJsonDict(self.site, profile)

        # this gets a SA session:
        sess = OrmManager().newSession()

        cbRecord = sess.merge(newRecord)
        cbRecord.modified = None        # a timestamp field that must be
updated
        sess.commit()
        sess.close()

The sess.merge() call raises that error. The test that fails tries to
merge a CbRecord having five persons, two of which are identical to
those already stored in the database (same primary key). I would
expect the result to be the database having the five persons stored
and related to the same CbRecord.

I tried to read the FAQ and googled a lot to no avail. Any help would
be highly appreciated.
Thanks in advance!
Yassen

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to