3 Dec 2008 18:23
Re: New instance ExtraStat with identity key (...) conflicts with persistent instance ExtraStat
Doug Farrell <dfarrell <at> mypublisher.com>
2008-12-03 17:23:46 GMT
2008-12-03 17:23:46 GMT
I've met so few other Farrells, maybe we ARE related!! Get it, related, in a SqlAlchemy group. Oh man I'm such a geek!! > -----Original Message----- > From: sqlalchemy <at> googlegroups.com [mailto:sqlalchemy <at> googlegroups.com] > On Behalf Of Bob > Sent: Tuesday, December 02, 2008 9:00 AM > To: sqlalchemy <at> googlegroups.com > Subject: [sqlalchemy] Re: New instance ExtraStat with identity key > (...) conflicts with persistent instance ExtraStat > > > I keep thinking one of my relatives has emailed me when you post here > and I get all excited. Any chance you can change your name ?> > 2008/12/2 Doug Farrell <dfarrell <at> mypublisher.com>: > > Michael, > > > > > > > > Thanks for the pointer, that makes great sense, and once again points > how my > > generally small database design skills. I'll update my code to try > this out. > > > > > > > > Again, > > > > Thanks! > > > > Doug > > > > > > > > From: sqlalchemy <at> googlegroups.com > [mailto:sqlalchemy <at> googlegroups.com] On > > Behalf Of Michael Bayer > > Sent: Saturday, November 29, 2008 9:28 PM > > To: sqlalchemy <at> googlegroups.com > > Subject: [sqlalchemy] Re: New instance ExtraStat with identity key > (...) > > conflicts with persistent instance ExtraStat > > > > > > > > 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 ExtraStat <at> ... With > identity > > > > key (<class '__main__.ExtraStat'>,(u'C',)) conflicts with persistent > > > > instance ExtraStat <at> ... > > > > > > > > Thanks! > > > > Doug > > > > > > > > > > > > > > > > > > > r more options, visit this group at > http://groups.google.com/group/sqlalchemy?hl=en > -~----------~----~----~----~------~----~------~--~--- --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy <at> googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscribe <at> googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~----------~----~----~----~------~----~------~--~---
>
> 2008/12/2 Doug Farrell <dfarrell <at> mypublisher.com>:
> > Michael,
> >
> >
> >
> > Thanks for the pointer, that makes great sense, and once again
points
> how my
> > generally small database design skills. I'll update my code to try
> this out.
> >
> >
> >
> > Again,
> >
> > Thanks!
> >
> > Doug
> >
> >
> >
> > From: sqlalchemy <at> googlegroups.com
> [mailto:sqlalchemy <at> googlegroups.com] On
> > Behalf Of Michael Bayer
> > Sent: Saturday, November 29, 2008 9:28 PM
> > To: sqlalchemy <at> googlegroups.com
> > Subject: [sqlalchemy] Re: New instance ExtraStat with identity key
> (...)
> > conflicts with persistent instance ExtraStat
> >
> >
> >
> > 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 ExtraStat <at> ... With
> identity
> >
> > key (<class '__main__.ExtraStat'>,(u'C',)) conflicts with persistent
> >
> > instance ExtraStat <at> ...
> >
> >
> >
> > Thanks!
> >
> > Doug
> >
> >
> >
> >
> >
> > >
> >
>
> >
> r more options, visit this group at
>
RSS Feed