在具有非主键的列上使用 SqlAlchemy 和 MySQL 设置 AUTO_INCRMENT?

发布于 2024-09-03 15:09:28 字数 1816 浏览 5 评论 0原文

我不知道如何使用 SqlAlchemy 0.6.0 和 MySQL 5 在 UNIQUE 列上设置 AUTO_INCRMENT。

我知道这可以在 MySQL 中完成,但我不想分发额外的 .sql 脚本来设置为我的应用程序建立数据库。我希望 SqlAlchemy 从 Python 中构建架构。

据我到目前为止发现,有两种方法可以使列成为 SqlAlchemy 中的自动递增类型列:

  1. 它是表中第一个 sqlalchemy.types.Integer 列,其中 < code>primary_key=True 并且在其定义中没有设置 autoincrement=False
    • 对于 MSSQL,这会向列添加 INDEX(m,n) 属性。
    • 对于 Postgres,这会导致使用 SERIAL 列类型。
    • 使用 MySQL 将 AUTO_INCRMENT 属性添加到列中。
  2. 它是一个 sqlalchemy.types.Integer 列,并使用 sqlalchemy.schema.Sequence 对象作为参数进行实例化。
    • 对于 MSSQL,这会向列添加 INDEX(m,n) 属性。
    • 对于 Postgres,这会导致使用 SERIAL 列类型。
    • 对于 MySQL,这似乎会被忽略。

所以我不能使用#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:

  1. It is the first sqlalchemy.types.Integer column in the table that has primary_key=True and does not have autoincrement=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.
  2. It is an sqlalchemy.types.Integer column, and is instantiated with an sqlalchemy.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.

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

牵你手 2024-09-10 15:09:28

摘自 http://www.mail-archive.com/[电子邮件受保护]/msg19744.html

目前您最好的选择是发出一个ALTER TABLE来应用
AUTO_INCRMENT,如果 MySQL 支持的话。

from sqlalchemy.schema import DDL
DDL("ALTER TABLE person ...", on='mysql').execute_at('after-create', 
    person_table)[..]

Excerpt from http://www.mail-archive.com/[email protected]/msg19744.html

Your best bet for now is to issue an ALTER TABLE that applies the
AUTO_INCREMENT, if MySQL supports that.

from sqlalchemy.schema import DDL
DDL("ALTER TABLE person ...", on='mysql').execute_at('after-create', 
    person_table)[..]
随遇而安 2024-09-10 15:09:28

您可能对 我需要在MySQL中自动递增一个非主键的字段

我对sqlalchemy不熟悉,但我知道这可以在MySQL中完成。您必须首先将 id 列定义为 PRIMARY KEY 并将其设置为 AUTO_INCRMENT。然后,您可以ALTER TABLEDROP PRIMARY KEY,将其替换为ADD UNIQUE KEY。这将保留该列上的 AUTO_INCRMENT 功能。然后,您可以在其他列上创建PRIMARY KEY

要分解它,您的第一步是这样创建表:

CREATE TABLE person (
    id INTEGER NOT NULL AUTO_INCREMENT,
    first_name VARCHAR(100) NOT NULL,
    last_name VARCHAR(100) NOT NULL,
    PRIMARY KEY (id)
);

接下来修改 id 键:

ALTER TABLE person DROP PRIMARY KEY, ADD UNIQUE KEY(id);

最后,在其他列上添加您的 PRIMARY KEY

ALTER TABLE person ADD PRIMARY KEY(first_name, last_name);

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 the PRIMARY KEY and set it AUTO_INCREMENT. Then you can ALTER TABLE and DROP PRIMARY KEY, replacing it with an ADD UNIQUE KEY. This will preserve the AUTO_INCREMENT functionality on that column. You can then create the PRIMARY KEY on the other columns.

To break it down, your first step is to create the table thusly:

CREATE TABLE person (
    id INTEGER NOT NULL AUTO_INCREMENT,
    first_name VARCHAR(100) NOT NULL,
    last_name VARCHAR(100) NOT NULL,
    PRIMARY KEY (id)
);

Next modify the id key:

ALTER TABLE person DROP PRIMARY KEY, ADD UNIQUE KEY(id);

Finally, add your PRIMARY KEY on the other columns:

ALTER TABLE person ADD PRIMARY KEY(first_name, last_name);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文