ORM attributed updated but not reflected in queries.

Issue #3369 resolved
Tim Henderson created an issue

This is for SQL Alchemy 1.0.0b1 - 1.0.0 final. Issue is not present in 0.9.9

The attached file reproduces the bug. It will print "passed test" if the bug is not present or "failed test" if the bug is present. You must first setup the database credentials in the test file.

The bug:

I create a new ORM object and then override a foreign key manually. I then add the row and commit it. In prior versions of SQLAlchemy (for instance 0.9.9) this will cause an integrity error as the child row is not present. In 1.0.0 the wrong attribute is rendered in the SQL insert query. This means no integrity error occurs and the insert succeeds. However, it has inserted the wrong data.

Enivronment:

  • pypy: 2.5.1 (cpython 2.7.9 compat)
  • SQLAlchemy: 1.0.0 (works in 0.9.9)
  • MySQL: 5.6

Comments (6)

  1. Mike Bayer repo owner

    Alex Grönholm made up a more succinct test case:

    import sqlalchemy as sa
    
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy.orm import Session
    
    Base = declarative_base()
    
    
    class A(Base):
        __tablename__ = 'table_a'
        __table_args__ = {'mysql_engine':'InnoDB'}
    
        id = sa.Column(sa.String(20), primary_key=True)
    
    
    class B(Base):
        __tablename__ = 'table_b'
        __table_args__ = {'mysql_engine':'InnoDB'}
    
        id = sa.Column(sa.Integer, primary_key=True)
        a_id = sa.Column(sa.String(20), sa.ForeignKey('table_a.id'))
        a = sa.orm.relationship(A, primaryjoin=a_id == A.id)
    
    
    engine = sa.create_engine("postgresql:///testdb")
    Base.metadata.create_all(engine)
    session = Session(engine)
    
    try:
        b = B()
        b.a = None
        b.a_id = 'arbitrary_key'
        session.add(b)
        session.flush()
    except sa.exc.IntegrityError:
        print "passed test"
    else:
        print "failed test"
    finally:
        Base.metadata.drop_all(engine)
    
  2. Log in to comment