如何在SQLAlchemy for SQLServer中为ID列定义起始值?

时间:2016-08-27 15:45:24

标签: python sql-server sqlalchemy pymssql

我在Flask和SQLServer之间连接SQLAlchemy。

我有一个ID列,我想从10000开始。我找不到答案。

下面是SQL代码,我正在尝试转换为SQLAlchemy。

请告诉我。

THX

create TABLE [dbo].[Product](
    [ID] [int] IDENTITY(**10000**,1) NOT NULL,
    [Name] [varchar](200) NOT NULL,
    [Description] [varchar](500) NULL,
    [Brand] [varchar](500) NOT NULL,
    [Price] [real] NOT NULL,
    [Qty] [int] NOT NULL,
    [ProductFilenamePrefix] [varchar](200) NOT NULL,
PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
UNIQUE NONCLUSTERED 
(
    [Name] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

1 个答案:

答案 0 :(得分:1)

在SQLAlchemy创建表之后,您需要进行一些更改,如上面针对MySQL的链接所示,但是使用SQL Server。

from sqlalchemy import event
from sqlalchemy import DDL
event.listen(
    Article.__table__,
    "after_create",
    DDL("DBCC checkident (%(table)s, reseed, 9999)")
)

这将使插入的下一条记录以10000的标识值开始。祝你好运。