fork(2) download
  1. import sqlalchemy as sa
  2. from sqlalchemy import orm
  3. import sqlalchemy.ext.declarative as decl
  4.  
  5. engine = sa.create_engine(
  6. 'sqlite:///:memory:'
  7.  
  8. # To see the same code working on a different engine, try this string:
  9. # 'postgresql://postgres:password@localhost/postgres'
  10. # with this docker container:
  11. # docker run -dp 5432:5432 -e POSTGRES_PASSWORD=password postgres:10-alpine
  12. )
  13.  
  14. Session = orm.sessionmaker(bind=engine)
  15.  
  16. if engine.dialect.name == 'postgresql':
  17. with engine.connect() as c:
  18. c.execute('CREATE EXTENSION IF NOT EXISTS "uuid-ossp"')
  19. c.execute("""
  20. CREATE TEMP TABLE test_table(
  21. id VARCHAR PRIMARY KEY NOT NULL
  22. DEFAULT uuid_generate_v4()::text
  23. );
  24. """)
  25. elif engine.dialect.name == 'sqlite':
  26. engine.execute("""
  27. CREATE TABLE test_table(
  28. id VARCHAR PRIMARY KEY NOT NULL
  29. DEFAULT (lower(hex(randomblob(16))))
  30. );
  31. """)
  32.  
  33. Base = decl.declarative_base()
  34.  
  35. class TestTable(Base):
  36. __tablename__ = 'test_table'
  37. id = sa.Column(
  38. sa.VARCHAR,
  39. primary_key=True,
  40. server_default=sa.FetchedValue())
  41.  
  42.  
  43. sess = Session()
  44. t = TestTable()
  45. sess.add(t)
  46.  
  47. # sqlalchemy.orm.exc.FlushError: Instance <TestTable at 0x7f66b41c0a10> has a
  48. # NULL identity key. If this is an auto-generated value, check that the
  49. # database table allows generation of new primary key values, and that the
  50. # mapped Column object is configured to expect these generated values. Ensure
  51. # also that this flush() is not occurring at an inappropriate time, such as
  52. # within a load() event.
  53. sess.commit()
  54.  
  55. # Only ever get this far on postgres
  56. print(t.id)
Runtime error #stdin #stdout #stderr 0.12s 60872KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "prog.py", line 53, in <module>
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 874, in commit
    self.transaction.commit()
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 461, in commit
    self._prepare_impl()
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 441, in _prepare_impl
    self.session.flush()
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 2139, in flush
    self._flush(objects)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 2259, in _flush
    transaction.rollback(_capture_exception=True)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/util/langhelpers.py", line 60, in __exit__
    compat.reraise(exc_type, exc_value, exc_tb)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 2229, in _flush
    flush_context.finalize_flush_changes()
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/unitofwork.py", line 411, in finalize_flush_changes
    self.session._register_newly_persistent(other)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 1598, in _register_newly_persistent
    % state_str(state)
sqlalchemy.orm.exc.FlushError: Instance <TestTable at 0x2b23c6770150> has a NULL identity key.  If this is an auto-generated value, check that the database table allows generation of new primary key values, and that the mapped Column object is configured to expect these generated values.  Ensure also that this flush() is not occurring at an inappropriate time, such aswithin a load() event.