On 03/10/2013 11:42, Werner wrote:
Hi,

I have a problem that when I add a record to 'cellarbook' using the following code the 'old' cellarbook entry is dropped from the db.

oldCB = session.query(db.Cellarbook).get(7)

newCB = db.Cellarbook()

# set some data from existing dbitem
newCB.cellar = oldCB.cellar
newCB.authuser = oldCB.authuser
newCB.supplier = oldCB.supplier
newCB.vintage = oldCB.vintage

newBot = db.Bottle()
newCB.vintage.bottle.append(newBot)
newCB.bottle = newBot

newCB.drinkinfo = oldCB.drinkinfo

session.add(newCB)
session.commit()

After this I have newCB.id=10 in the database but oldCB.id=7 has gone.

I guess/think that I have a relation setup incorrectly which creates this effect, but I can not put my finger on it.

Appreciate any hint.
Werner

The relation setup for the classes involved, and I suspect Vintage.drinkinfo/Cellarbook.drinkinfo ones:

Vintage.vintadd = sao.relationship('Vintadd', uselist=False,
                                    cascade="all, delete, delete-orphan",
                                    single_parent=True)
Vintage.cellarbook = sao.relationship('Cellarbook', uselist=False,
cascade="all, delete, delete-orphan",
                                      backref='vintage')
Vintage.drinkinfo = sao.relationship('Drinkinfo', backref='vintage',
cascade="all, delete, delete-orphan",
                                     single_parent=True)

Bottle.cellarbook = sao.relationship('Cellarbook', uselist=False,
#cascade="all, delete, delete-orphan",
                                     backref='bottle')
Bottle.vintage = sao.relationship('Vintage', backref='bottle',
                                  #cascade="all, delete, delete-orphan",
                                  single_parent=True)

Cellarbook.supplier = sao.relationship('Profilec',
primaryjoin=('Cellarbook.fk_supplier_id==Profilec.id'),
                        viewonly=True)
Cellarbook.cellar = sao.relationship('Cellar', backref='cellarbook')
Cellarbook.authuser = sao.relationship('Authuser')
Cellarbook.drinkinfo = sao.relationship('Drinkinfo', backref='cellarbook',
                                        single_parent=True)
Cellarbook.purchase = sao.relationship('Purchase', backref='cellarbook',
cascade="all, delete, delete-orphan")

Purchase.cellar = sao.relationship('Cellar')
Purchase.currency = sao.relationship('Currency')
Purchase.supplier = sao.relationship('Profilec',
primaryjoin=('Purchase.fk_supplier_id==Profilec.id'),
                                     viewonly=True)

If I create a new 'vintage' record then it works (see code below), but that is another use case and I really would like to support both of them.

Werner

newCB = db.Cellarbook()

# set some data from existing dbitem
newCB.cellar = oldCB.cellar
newCB.authuser = oldCB.authuser
newCB.supplier = oldCB.supplier
newCB.drinkinfo = oldCB.drinkinfo

## use existing vintage
#newCB.vintage = oldCB.vintage

# create a new vintage
dbVintage = db.Vintage()
dbVintage.vintage = 2025
dbVintAdd = db.Vintadd()
dbVintage.vintadd = dbVintAdd
dbVintage.drinkinfo = newCB.drinkinfo
newCB.vintage = dbVintage

# create a bottle
newBot = db.Bottle()
newCB.vintage.bottle.append(newBot)
newCB.bottle = newBot

session.add(newCB)
session.flush()

session.commit()

--
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to