Simon's suggestion about the duplicate "name" still holds.  Your  
relation from Stat->ExtraStat currently needs to be one-to-one since  
you cannot have more than one ExtraStat referencing a single Stat, due  
to the PK constraint on ExtraStat.name.  The error is raising at the  
point of query() since autoflush is kicking in - use session.flush()  
to isolate the error.

On Nov 29, 2008, at 12:18 PM, Doug Farrell wrote:

> Hi all,
>
> I'm having a problem with a new instance of a relation conflicting  
> with
> an existing instance. I'm using SA 0.5rc with Sqlite3. Here are my
> simplified classes:
>
> class Stat(sqladb.Base):
>      __tablename__ = "stats"
>      name         = Column(String(32), primary_key=True)
>      total        = Column(Integer)
>      created      = Column(DateTime, default=datetime.datetime.now())
>      updated      = Column(DateTime)
>      states       = Column(PickleType, default={})
>      extraStats   = relation("ExtraStat", backref="stat")
>
> class ExtraStat(sqladb.Base):
>      __tablename__ = "extrastats"
>      name         = Column(String(32), ForeignKey("stats.name"),  
> primary_key=True)
>      total        = Column(Integer)
>      created      = Column(DateTime, default=datetime.datetime.now())
>      updated      = Column(DateTime)
>      states       = Column(PickleType, default={})
>
> The above Stat class has a one-to-many relationship with the ExtraStat
> class (which I think I've implemented correctly). Later in the
> program I create an in memory data model that has as part of it's
> components two
> dictionaries that contain Stat instances. Those Stat instances have
> relationships to ExtraStat instances. My problem comes in the
> following when I'm trying to update the data in those instances/ 
> tables.
> Here is a section of code that throws the exception:
>
>
>
> pressName = "press%s" % pressNum
> # add new ExtraStat instances as relations
> self._addProductStatsPress(productType, pressName)
> self._addPressStatsProduct(pressName, productType)
> try:
>    extraStat = session.query(Stat). \
>                filter(Stat.name==productType). \
>                join("extraStats"). \
>                filter(ExtraStat.name==pressName).one()
> except:
>    extraStat = ExtraStat(pressName, ExtraStat.PRESS_TYPE)
>    self.productStats[productType].extraStats.append(extraStat)
>    extraStat.states.setdefault(sstate, 0)
>    extraStat.states[sstate] += 1
>    extraStat.updated = now
>    extraStat = session.merge(extraStat)
> try:
>    extraStat = session.query(Stat). \
>                filter(Stat.name==pressName). \
>                join("extraStats"). \
>                filter(ExtraStat.name==productType).one()   <====  
> throws exception right here
> except:
>    extraStat = ExtraStat(productType, ExtraStat.PRODUCT_TYPE)
>    self.pressStats[pressName].extraStats.append(extraStat)
>    extraStat.states.setdefault(sstate, 0)
>    extraStat.states[sstate] += 1
>    extraStat.updated = now
>
> The marked area is wear it throws the exception. I'm not sure what to
> do here to get past this, any help or ideas would be greatly
> appreciated.
>
> The exact exception is as follows:
> Sqlalchemy.orm.exc.FlushError: New instance [EMAIL PROTECTED] With  
> identity
> key (<class '__main__.ExtraStat'>,(u'C',)) conflicts with persistent
> instance [EMAIL PROTECTED]
>
> Thanks!
> Doug
>
> >


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
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