Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. sqlalchemy.exc.IntegrityError: (raised as a result of Query-invoked autoflush;
  2. consider using a session.no_autoflush block if this flush is occurring prematurely)
  3. (sqlite3.IntegrityError) NOT NULL constraint failed: X.nn
  4. [SQL: 'INSERT INTO "X" (nn, val) VALUES (?, ?)'] [parameters: (None, 1)]
  5.  
  6. #!/usr/bin/env python3
  7.  
  8. import os.path
  9. import os
  10. import sqlalchemy as sa
  11. import sqlalchemy.orm as sao
  12. import sqlalchemy.ext.declarative as sad
  13. from sqlalchemy_utils import create_database
  14.  
  15. _Base = sad.declarative_base()
  16. session = None
  17.  
  18.  
  19. class X(_Base):
  20. __tablename__ = 'X'
  21.  
  22. _oid = sa.Column('oid', sa.Integer, primary_key=True)
  23. _nn = sa.Column('nn', sa.Integer, nullable=False) # NOT NULL!
  24. _val = sa.Column('val', sa.Integer)
  25.  
  26. def __init__(self, val):
  27. self._val = val
  28.  
  29. def test(self, session):
  30. q = session.query(X).filter(X._val == self._val)
  31. x = q.one()
  32. print('x={}'.format(x))
  33.  
  34. dbfile = 'x.db'
  35.  
  36. def _create_database():
  37. if os.path.exists(dbfile):
  38. os.remove(dbfile)
  39.  
  40. engine = sa.create_engine('sqlite:///{}'.format(dbfile), echo=True)
  41. create_database(engine.url)
  42. _Base.metadata.create_all(engine)
  43. return sao.sessionmaker(bind=engine)()
  44.  
  45.  
  46. if __name__ == '__main__':
  47. session = _create_database()
  48.  
  49. for val in range(3):
  50. x = X(val)
  51. x._nn = 0
  52. session.add(x)
  53. session.commit()
  54.  
  55. x = X(1)
  56. session.add(x)
  57. x.test(session)
  58.  
  59. def test(self, session):
  60. with session.no_autoflush:
  61. q = session.query(X).filter(X._val == self._val)
  62. x = q.one()
  63. print('x={}'.format(x))
  64.  
  65. return sao.sessionmaker(bind=engine, autoflush=False)()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement