如何在SQLAlchemy中对整列实施唯一性约束?

时间:2022-11-24 20:25:19

I have the following MySQL class.

我有以下MySQL类。

class MyClassA(db.Model):
    b = db.Column(db.String(12))
    c = db.Column(db.String(12))

I want to make sure that the values of b are unique over the whole column. How can I change this declaration to enforce that constraint?

我想确保b的值在整个列中是唯一的。如何更改此声明以强制执行该约束?

1 个解决方案

#1


2  

Well, you can use the flag "unique=True" like flask sql alchemy's documentation:

那么,您可以使用标志“unique = True”,如flask sql alchemy的文档:

class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    name = Column(String(50), **unique=True**)
    email = Column(String(120), unique=True)

Flask SQL Alchemy

Flask SQL Alchemy

I hope this helps

我希望这有帮助

#1


2  

Well, you can use the flag "unique=True" like flask sql alchemy's documentation:

那么,您可以使用标志“unique = True”,如flask sql alchemy的文档:

class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    name = Column(String(50), **unique=True**)
    email = Column(String(120), unique=True)

Flask SQL Alchemy

Flask SQL Alchemy

I hope this helps

我希望这有帮助