Flask SQL Alchemy 在不需要时尝试执行 SET 操作
Flask SQL Alchemy try perform SET operation, when it is not needed
我在使用 SQL Alchemy 时遇到了一个非常奇怪的问题。我在 SQL 服务器中有一个 table,用于存储销售额,格式如下:
CREATE TABLE Resale (
[address] VARCHAR(100) NOT NULL,
[id] BIGINT NOT NULL,
[price] INT NOT NULL,
[start_date] DATETIME2(7) NOT NULL DEFAULT GETUTCDATE(),
[sys_start_time] DATETIME2 (7) GENERATED ALWAYS AS ROW START HIDDEN NOT NULL,
[sys_end_time] DATETIME2 (7) GENERATED ALWAYS AS ROW END HIDDEN NOT NULL,
PERIOD FOR SYSTEM_TIME ([sys_start_time], [sys_end_time]),
CONSTRAINT [PK_ReSale] PRIMARY KEY CLUSTERED ([id] ASC))
WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE=[dbo].[ResaleHistory], DATA_CONSISTENCY_CHECK=ON));
在 python 我使用了 flask-sqlalchemy 并构建了这个模型:
class Resale(db.Model):
__tablename__ = 'Resale'
address = db.Column(db.String(100), nullable=False)
id = db.Column(db.BigInteger, primary_key=True)
price = db.Column(db.Integer, nullable=False)
start_date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow())
现在我正尝试使用 :
在此 table 中插入一条记录
resale = Resale(address=address, id=id, price=price)
db.session.add(resale)
db.session.commit()
和烧瓶 return :
pyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Table 'Resale' does not have the identity property. Cannot perform SET operation. (8106) (SQLExecDirectW)")
[SQL: SET IDENTITY_INSERT [Resale] ON]
我真的不明白为什么我有那个,我没有在我的 table 中使用自动增量,所以我的烧瓶要我做“SET IDENTITY_INSERT Resale ON ?
老实说,我完全迷失了这条消息,我只是想在数据库中添加一条记录
如 中所述,您应该考虑在您的“id”上将标志“autoincrement”设置为 False。
我在使用 SQL Alchemy 时遇到了一个非常奇怪的问题。我在 SQL 服务器中有一个 table,用于存储销售额,格式如下:
CREATE TABLE Resale (
[address] VARCHAR(100) NOT NULL,
[id] BIGINT NOT NULL,
[price] INT NOT NULL,
[start_date] DATETIME2(7) NOT NULL DEFAULT GETUTCDATE(),
[sys_start_time] DATETIME2 (7) GENERATED ALWAYS AS ROW START HIDDEN NOT NULL,
[sys_end_time] DATETIME2 (7) GENERATED ALWAYS AS ROW END HIDDEN NOT NULL,
PERIOD FOR SYSTEM_TIME ([sys_start_time], [sys_end_time]),
CONSTRAINT [PK_ReSale] PRIMARY KEY CLUSTERED ([id] ASC))
WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE=[dbo].[ResaleHistory], DATA_CONSISTENCY_CHECK=ON));
在 python 我使用了 flask-sqlalchemy 并构建了这个模型:
class Resale(db.Model):
__tablename__ = 'Resale'
address = db.Column(db.String(100), nullable=False)
id = db.Column(db.BigInteger, primary_key=True)
price = db.Column(db.Integer, nullable=False)
start_date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow())
现在我正尝试使用 :
在此 table 中插入一条记录resale = Resale(address=address, id=id, price=price)
db.session.add(resale)
db.session.commit()
和烧瓶 return :
pyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Table 'Resale' does not have the identity property. Cannot perform SET operation. (8106) (SQLExecDirectW)")
[SQL: SET IDENTITY_INSERT [Resale] ON]
我真的不明白为什么我有那个,我没有在我的 table 中使用自动增量,所以我的烧瓶要我做“SET IDENTITY_INSERT Resale ON ?
老实说,我完全迷失了这条消息,我只是想在数据库中添加一条记录
如