Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- sqlalchemy.exc.IntegrityError: (raised as a result of Query-invoked autoflush;
- consider using a session.no_autoflush block if this flush is occurring prematurely)
- (sqlite3.IntegrityError) NOT NULL constraint failed: X.nn
- [SQL: 'INSERT INTO "X" (nn, val) VALUES (?, ?)'] [parameters: (None, 1)]
- #!/usr/bin/env python3
- import os.path
- import os
- import sqlalchemy as sa
- import sqlalchemy.orm as sao
- import sqlalchemy.ext.declarative as sad
- from sqlalchemy_utils import create_database
- _Base = sad.declarative_base()
- session = None
- class X(_Base):
- __tablename__ = 'X'
- _oid = sa.Column('oid', sa.Integer, primary_key=True)
- _nn = sa.Column('nn', sa.Integer, nullable=False) # NOT NULL!
- _val = sa.Column('val', sa.Integer)
- def __init__(self, val):
- self._val = val
- def test(self, session):
- q = session.query(X).filter(X._val == self._val)
- x = q.one()
- print('x={}'.format(x))
- dbfile = 'x.db'
- def _create_database():
- if os.path.exists(dbfile):
- os.remove(dbfile)
- engine = sa.create_engine('sqlite:///{}'.format(dbfile), echo=True)
- create_database(engine.url)
- _Base.metadata.create_all(engine)
- return sao.sessionmaker(bind=engine)()
- if __name__ == '__main__':
- session = _create_database()
- for val in range(3):
- x = X(val)
- x._nn = 0
- session.add(x)
- session.commit()
- x = X(1)
- session.add(x)
- x.test(session)
- def test(self, session):
- with session.no_autoflush:
- q = session.query(X).filter(X._val == self._val)
- x = q.one()
- print('x={}'.format(x))
- return sao.sessionmaker(bind=engine, autoflush=False)()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement