Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7058561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:07:38+00:00 2026-05-28T04:07:38+00:00

I have two tables, testInstance and bugzilla that are associated by a third one,

  • 0

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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:07:39+00:00Added an answer on May 28, 2026 at 4:07 am

    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)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables, one that contains volunteers, and one that contains venues. Volunteers
I have two tables: users and user_depts. Let's say (for this question) that users
I have two tables with this structure: Table one: ID Description Table two: ID
I have two tables and want to compare rows on sqlite like this table1
I have two tables. With Sqlalchemy, I map them to two classes: class A(base):
I have two tables, like this: #Articles: ID | Title 1 Article title 2
I have two tables looking like this: A B id_attr value id id_attr value
I have two tables built like this (this is just a simplified and non-proprietary
I have two tables that are joined together. A has many B Normally you
I have two tables say, t1 and t2 that have t1.t1_id and t2.t2_id as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.