sqlalchemy-history
sqlalchemy-history copied to clipboard
Identity based columns causing Issue when tested with MSSQL
When running tests by making id column Identity test starts failing when tested against MSSql DB v 2017
test case
class Article(self.Model):
__tablename__ = "article"
__versioned__ = copy(self.options)
id = sa.Column(
sa.Integer, sa.Identity(), autoincrement=True, primary_key=True
)
name = sa.Column(sa.Unicode(255), nullable=False)
content = sa.Column(sa.UnicodeText)
description = sa.Column(sa.UnicodeText)
# Dynamic column cotaining all text content data
fulltext_content = column_property(name + content + description)
#### Testcase flow that fails.
def test_previous_for_first_version(self):
article = self.Article()
article.name = "Some article"
article.content = "Some content"
self.session.add(article)
self.session.commit()
DDL
CREATE TABLE article_version (
id INTEGER NOT NULL IDENTITY,
name NVARCHAR(255) NULL,
content NVARCHAR(max) NULL,
description NVARCHAR(max) NULL,
transaction_id BIGINT NOT NULL,
operation_type SMALLINT NOT NULL,
PRIMARY KEY (id, transaction_id)
)
sqlalchemy.exc.OperationalError: (pymssql._pymssql.OperationalError) (544, b"Cannot insert explicit value for identity column in table 'article_version' when IDENTITY_INSERT is set to OFF.DB-Lib error message 20018, severity 16:\nGeneral SQL Server error: Check messages from the SQL Server\n")
E [SQL: INSERT INTO article_version (id, name, content, description, transaction_id, operation_type) VALUES (%(id)s, %(name)s, %(content)s, %(description)s, %(transaction_id)s, %(operation_type)s)]
after some hit and try and feedbacks able to make it work by setting
column_copy.identity = None
in table_builder. reflect_Column method but some corner case test is failing where a related object has Identity column,
test case tests/inheritance/test_join_table_inheritance.py::TestDeepJoinedTableInheritance::test_insert
error:
E sqlalchemy.exc.OperationalError: (pymssql._pymssql.OperationalError) (544, b"Cannot insert explicit value for identity column in table 'content' when IDENTITY_INSERT is set to OFF.DB-Lib error message 20018, severity 16:\nGeneral SQL Server error: Check messages from the SQL Server\n")
E [SQL: INSERT INTO content (id, description) VALUES (%(id)s, %(description)s)]
E [parameters: {'id': 1, 'description': None}]
E (Background on this error at: https://sqlalche.me/e/14/e3q8)