sqlalchemy 在多个列中是唯一的

问题描述 投票:0回答:4

假设我有一个代表位置的类。地点“属于”客户。位置由 unicode 10 字符代码标识。 “位置代码”在特定客户的位置中应该是唯一的。

The two below fields in combination should be unique
customer_id = Column(Integer,ForeignKey('customers.customer_id')
location_code = Column(Unicode(10))

因此,如果我有两个客户,客户“123”和客户“456”。它们都可以有一个名为“main”的位置,但都不能有两个名为 main 的位置。

我可以在业务逻辑中处理这个问题,但我想确保无法在 sqlalchemy 中轻松添加需求。 unique=True 选项似乎仅在应用于特定字段时才起作用,并且它会导致整个表的所有位置仅具有唯一的代码。

python sqlalchemy
4个回答
451
投票

摘自 Column

文档

unique – 当 True 时,表示该列包含唯一的 约束,或者如果 index 也为 True,则表明 Index 应该使用唯一的标志创建。指定多个列 约束/索引或指定显式名称,请使用 UniqueConstraintIndex 显式构造。

由于这些属于表而不属于映射的类,因此可以在表定义中声明它们,或者如果使用声明式,如

__table_args__
:

# version1: table definition
mytable = Table('mytable', meta,
    # ...
    Column('customer_id', Integer, ForeignKey('customers.customer_id')),
    Column('location_code', Unicode(10)),

    UniqueConstraint('customer_id', 'location_code', name='uix_1')
    )
# or the index, which will ensure uniqueness as well
Index('myindex', mytable.c.customer_id, mytable.c.location_code, unique=True)


# version2: declarative
class Location(Base):
    __tablename__ = 'locations'
    id = Column(Integer, primary_key = True)
    customer_id = Column(Integer, ForeignKey('customers.customer_id'), nullable=False)
    location_code = Column(Unicode(10), nullable=False)
    __table_args__ = (UniqueConstraint('customer_id', 'location_code', name='_customer_location_uc'),
                     )

47
投票
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()

class Location(Base):
      __table_args__ = (
        # this can be db.PrimaryKeyConstraint if you want it to be a primary key
        db.UniqueConstraint('customer_id', 'location_code'),
      )
      customer_id = Column(Integer,ForeignKey('customers.customer_id')
      location_code = Column(Unicode(10))

3
投票

这个使用 Flask sqlalchemy 的 Python3 答案是完全衍生的,它只是将上面的所有内容放入 MySQL 的一个小型独立工作示例中。我需要对表进行唯一性约束来实现多对多关系。也许你可以运行它来调试本地环境问题,在我的例子中,它们纯粹是在键盘和椅子之间:)

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import Column, ForeignKey, Integer, String, UniqueConstraint
from sqlalchemy.orm import relationship
import logging

logging.basicConfig()
logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+mysqlconnector://user:pass@localhost/mydb'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)

user_role = db.Table(
    'user_role',
    Column('uid', String(6), ForeignKey('user.uid')),
    Column('role', String(32), ForeignKey('role.name')),
    UniqueConstraint('uid', 'role', name='idx_uid_role'))

class UserModel(db.Model):
    __tablename__ = 'user'
    uid = Column(String(6), primary_key=True)
    create_time = Column(Integer, nullable=False)
    login_time = Column(Integer, nullable=True)
    roles = relationship('RoleModel', secondary='user_role',
                         backref='user', lazy='joined')

class RoleModel(db.Model):
    __tablename__ = 'role'
    name = Column(String(32), primary_key=True)
    description = Column(String(256), nullable=False)

db.create_all()

运行此命令后,检查为表定义的索引,如下所示:

mysql> show index from user_role;

你应该看到:

+-----------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table     | Non_unique | Key_name     | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+-----------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| user_role |          0 | idx_uid_role |            1 | uid         | A         |           0 |     NULL |   NULL | YES  | BTREE      |         |               | YES     | NULL       |
| user_role |          0 | idx_uid_role |            2 | role        | A         |           0 |     NULL |   NULL | YES  | BTREE      |         |               | YES     | NULL       |
| user_role |          1 | role         |            1 | role        | A         |           0 |     NULL |   NULL | YES  | BTREE      |         |               | YES     | NULL       |
+-----------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
3 rows in set (0.00 sec)

创建测试数据:

mysql> insert into user values ('abc123', 1, 2);
mysql> insert into role values ('role1', 'Description');
mysql> insert into user_role (uid, role) values ('abc123', 'role1');

最后,通过第二次运行最后一个插入来测试唯一性约束,您应该看到:

mysql> insert into user_role (uid, role) values ('abc123', 'role1');
ERROR 1062 (23000): Duplicate entry 'abc123-role1' for key 'user_role.idx_uid_role'

2
投票
__table_args__ = (
    Index("idx_room_user", room_id, uid, unique=True),
    UniqueConstraint(room_id, uid, name='u_room_user'),
)

它在 sqlalchemy 2.0 下工作

© www.soinside.com 2019 - 2024. All rights reserved.