在具有非主键的列上使用 SqlAlchemy 和 MySQL 设置 AUTO_INCRMENT?
我不知道如何使用 SqlAlchemy 0.6.0 和 MySQL 5 在 UNIQUE 列上设置 AUTO_INCRMENT。
我知道这可以在 MySQL 中完成,但我不想分发额外的 .sql 脚本来设置为我的应用程序建立数据库。我希望 SqlAlchemy 从 Python 中构建架构。
据我到目前为止发现,有两种方法可以使列成为 SqlAlchemy 中的自动递增类型列:
- 它是表中第一个
sqlalchemy.types.Integer
列,其中 < code>primary_key=True 并且在其定义中没有设置autoincrement=False
。- 对于 MSSQL,这会向列添加
INDEX(m,n)
属性。 - 对于 Postgres,这会导致使用
SERIAL
列类型。 - 使用 MySQL 将
AUTO_INCRMENT
属性添加到列中。
- 对于 MSSQL,这会向列添加
- 它是一个 sqlalchemy.types.Integer 列,并使用 sqlalchemy.schema.Sequence 对象作为参数进行实例化。
- 对于 MSSQL,这会向列添加
INDEX(m,n)
属性。 - 对于 Postgres,这会导致使用
SERIAL
列类型。 - 对于 MySQL,这似乎会被忽略。
- 对于 MSSQL,这会向列添加
所以我不能使用#1,因为我想要自动增量的列不在主键中(也不应该在主键中)。我不能使用 #2,因为它不适用于 MySQL。
基本上,我定义表的代码如下所示:
from sqlalchemy import Column
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.schema import Sequence
from sqlalchemy.types import Integer, Unicode
Base = declarative_base()
class Person(Base):
__tablename__ = "person"
id = Column(Integer, nullable=False, unique=True)
first_name = Column(Unicode(100), nullable=False, primary_key=True)
last_name = Column(Unicode(100), nullable=False, primary_key=True)
并生成此 SQL 来创建表:
CREATE TABLE person (
id INTEGER NOT NULL,
first_name VARCHAR(100) NOT NULL,
last_name VARCHAR(100) NOT NULL,
PRIMARY KEY (first_name, last_name),
UNIQUE (id)
)
我该怎么做才能让它生成以下 SQL?
CREATE TABLE person (
id INTEGER NOT NULL AUTO_INCREMENT,
first_name VARCHAR(100) NOT NULL,
last_name VARCHAR(100) NOT NULL,
PRIMARY KEY (first_name, last_name),
UNIQUE (id)
)
I can't figure out how to set AUTO_INCREMENT on a UNIQUE column using SqlAlchemy 0.6.0 with MySQL 5.
I know this can be done in MySQL, but I don't want to have to distribute extra .sql scripts in order to set up the databases for my application. I want SqlAlchemy to build the schema from within Python.
As far as I have found so far, there are two ways that a column can become an auto-incrementing type column in SqlAlchemy:
- It is the first
sqlalchemy.types.Integer
column in the table that hasprimary_key=True
and does not haveautoincrement=False
set in its definition.- With MSSQL this adds an
INDEX(m,n)
property to the column. - With Postgres this causes the
SERIAL
column type to be used. - With MySQL adds the
AUTO_INCREMENT
property to the column.
- With MSSQL this adds an
- It is an
sqlalchemy.types.Integer
column, and is instantiated with ansqlalchemy.schema.Sequence
object as an argument.- With MSSQL this adds an
INDEX(m,n)
property to the column. - With Postgres this causes the
SERIAL
column type to be used. - With MySQL this seems to get ignored.
- With MSSQL this adds an
So I can't use #1 because the column I want to auto_increment isn't in the primary key (nor should it be). And I can't use #2 because It doesn't work with MySQL.
Basically, my code to define the table looks like the following:
from sqlalchemy import Column
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.schema import Sequence
from sqlalchemy.types import Integer, Unicode
Base = declarative_base()
class Person(Base):
__tablename__ = "person"
id = Column(Integer, nullable=False, unique=True)
first_name = Column(Unicode(100), nullable=False, primary_key=True)
last_name = Column(Unicode(100), nullable=False, primary_key=True)
And produces this SQL to create the table:
CREATE TABLE person (
id INTEGER NOT NULL,
first_name VARCHAR(100) NOT NULL,
last_name VARCHAR(100) NOT NULL,
PRIMARY KEY (first_name, last_name),
UNIQUE (id)
)
What can I do to get it to produce the following SQL?
CREATE TABLE person (
id INTEGER NOT NULL AUTO_INCREMENT,
first_name VARCHAR(100) NOT NULL,
last_name VARCHAR(100) NOT NULL,
PRIMARY KEY (first_name, last_name),
UNIQUE (id)
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
摘自 http://www.mail-archive.com/[电子邮件受保护]/msg19744.html
Excerpt from http://www.mail-archive.com/[email protected]/msg19744.html
您可能对 我需要在MySQL中自动递增一个非主键的字段
我对sqlalchemy不熟悉,但我知道这可以在MySQL中完成。您必须首先将
id
列定义为PRIMARY KEY
并将其设置为AUTO_INCRMENT
。然后,您可以ALTER TABLE
和DROP PRIMARY KEY
,将其替换为ADD UNIQUE KEY
。这将保留该列上的AUTO_INCRMENT
功能。然后,您可以在其他列上创建PRIMARY KEY
。要分解它,您的第一步是这样创建表:
接下来修改
id
键:最后,在其他列上添加您的
PRIMARY KEY
:You might be interested in I need to auto_increment a field in MySQL that is not primary key
I'm not familiar with sqlalchemy, but I do know this can be accomplished in MySQL. You must first define the
id
column as thePRIMARY KEY
and set itAUTO_INCREMENT
. Then you canALTER TABLE
andDROP PRIMARY KEY
, replacing it with anADD UNIQUE KEY
. This will preserve theAUTO_INCREMENT
functionality on that column. You can then create thePRIMARY KEY
on the other columns.To break it down, your first step is to create the table thusly:
Next modify the
id
key:Finally, add your
PRIMARY KEY
on the other columns: