deubeuliou
deubeuliou

Reputation: 174

Add an object associating two existing objects

I have two tables, testInstance and bugzilla that are associated by a third one, bzCheck, like this:

class Instance(Base):
    __tablename__ = "testInstance"

    id = Column(Integer, primary_key=True)

    bz_checks = relation(BZCheck, backref="instance")

class BZCheck(Base):
    __tablename__ = "bzCheck"

    instance_id = Column(Integer, ForeignKey("testInstance.id"), primary_key=True)
    bz_id = Column(Integer, ForeignKey("bugzilla.id"), primary_key=True)
    status = Column(String, nullable=False)

    bug = relation(Bugzilla, backref="checks")

class Bugzilla(Base):
    __tablename__ = "bugzilla"

    id = Column(Integer, primary_key=True)

The backend is a postgresql server ; I'm using SQLalchemy 0.5

If I create the Instance, Bugzilla and BZCheck ojects, then do

bzcheck.bug = bugzilla
instance.bz_checks.append(bzcheck)

and then add and commit them ; everything is fine.

But now, let's assume I have an existing instance and an existing bugzilla and want to associate them:

instance = session.query(Instance).filter(Instance.id == 31).one()
bugzilla = session.query(Bugzilla).filter(Bugzilla.id == 19876).one()
check = BZCheck(status="OK")
check.bug = bugzilla
instance.bz_checks.append(check)

It fails:

In [6]: instance.bz_checks.append(check)
2012-01-09 18:43:50,713 INFO sqlalchemy.engine.base.Engine.0x...3bd0 select nextval('"bzCheck_instance_id_seq"')
2012-01-09 18:43:50,713 INFO sqlalchemy.engine.base.Engine.0x...3bd0 None
2012-01-09 18:43:50,713 INFO sqlalchemy.engine.base.Engine.0x...3bd0 ROLLBACK

It tries to get a new ID from an unexisting sequence instead of using the foreign key "testInstance.id"... I don't understand why. I have had similar problems when trying to modify objects after commiting them ; I should have missed something fundamental but what ?

Upvotes: 2

Views: 625

Answers (1)

zzzeek
zzzeek

Reputation: 75167

the part you're missing here is the stack trace. Always look at the stack trace - what is critical here is that it's autoflush, produced by the access of instance.bz_checks:

Traceback (most recent call last):
  File "test.py", line 44, in <module>
    instance.bz_checks.append(check)
  File "/Users/classic/dev/sqlalchemy/lib/sqlalchemy/orm/attributes.py", line 168, in __get__
    return self.impl.get(instance_state(instance),dict_)
  File "/Users/classic/dev/sqlalchemy/lib/sqlalchemy/orm/attributes.py", line 453, in get
    value = self.callable_(state, passive)
  File "/Users/classic/dev/sqlalchemy/lib/sqlalchemy/orm/strategies.py", line 563, in _load_for_state
    result = q.all()
  File "/Users/classic/dev/sqlalchemy/lib/sqlalchemy/orm/query.py", line 1983, in all
    return list(self)
  File "/Users/classic/dev/sqlalchemy/lib/sqlalchemy/orm/query.py", line 2092, in __iter__
    self.session._autoflush()
  File "/Users/classic/dev/sqlalchemy/lib/sqlalchemy/orm/session.py", line 973, in _autoflush
    self.flush()
  File "/Users/classic/dev/sqlalchemy/lib/sqlalchemy/orm/session.py", line 1547, in flush
    self._flush(objects)
  File "/Users/classic/dev/sqlalchemy/lib/sqlalchemy/orm/session.py", line 1616, in _flush
    flush_context.execute()
  File "/Users/classic/dev/sqlalchemy/lib/sqlalchemy/orm/unitofwork.py", line 328, in execute
    rec.execute(self)
  File "/Users/classic/dev/sqlalchemy/lib/sqlalchemy/orm/unitofwork.py", line 472, in execute
    uow
  File "/Users/classic/dev/sqlalchemy/lib/sqlalchemy/orm/mapper.py", line 2291, in _save_obj
    execute(statement, params)
  File "/Users/classic/dev/sqlalchemy/lib/sqlalchemy/engine/base.py", line 1405, in execute
    params)
  File "/Users/classic/dev/sqlalchemy/lib/sqlalchemy/engine/base.py", line 1538, in _execute_clauseelement
    compiled_sql, distilled_params
  File "/Users/classic/dev/sqlalchemy/lib/sqlalchemy/engine/base.py", line 1646, in _execute_context
    context)
  File "/Users/classic/dev/sqlalchemy/lib/sqlalchemy/engine/base.py", line 1639, in _execute_context
    context)
  File "/Users/classic/dev/sqlalchemy/lib/sqlalchemy/engine/default.py", line 330, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.IntegrityError: (IntegrityError) null value in column "instance_id" violates not-null constraint
 'INSERT INTO "bzCheck" (bz_id, status) VALUES (%(bz_id)s, %(status)s) RETURNING "bzCheck".instance_id' {'status': 'OK', 'bz_id': 19876}

you can see this because the line of code is:

instance.bz_checks.append(check)

then autoflush:

self.session._autoflush()
File "/Users/classic/dev/sqlalchemy/lib/sqlalchemy/orm/session.py", line 973, in _autoflush

Three solutions:

a. temporarily disable autoflush (see http://www.sqlalchemy.org/trac/wiki/UsageRecipes/DisableAutoflush)

b. ensure that the BZCheck association object is always created with it's full state needed before accessing any collections:

BZState(bug=bugzilla, instance=instance)

(this is usually a good idea for association objects - they represent the association between two points so it's most appropriate that they be instantiated with this state)

c. change the cascade rules so that the operation of check.bug = somebug doesn't actually place check into the session just yet. You can do this with cascade_backrefs, described at http://www.sqlalchemy.org/docs/orm/session.html#controlling-cascade-on-backrefs. (but you'd need to be on 0.6 or 0.7)

Upvotes: 2

Related Questions