Моя проблема в том, что я хотел бы обеспечить уникальность в двух ситуациях:
- , client и option не равны нулю
Код: Выделить всё
custom_id - и option не равны нулю, но custom_id имеет значение NULL.
Код: Выделить всё
client
Код: Выделить всё
class OptionTable(Base):
__tablename__ = "option_table"
__table_args__ = (
UniqueConstraint("custom", "client", "option", name="uix_custom_client_option"),
)
id = Column(Integer, primary_key=True)
custom_id = Column(Integer, ForeignKey("custom.id"), nullable=True)
client = Column(String, nullable=False)
option = Column(String, nullable=False)
Код: Выделить всё
+----+----------+----------+--------+---------------------------------------------+
| id | CustomID | Client | Option | result |
+----+----------+----------+--------+---------------------------------------------+
| 1 | 123 | MegaCorp | Apple | OK |
| 2 | 123 | MegaCorp | Apple | not unique |
| 3 | NULL | MegaCorp | Apple | OK |
| 4 | NULL | MegaCorp | Google | OK |
| 5 | NULL | MegaCorp | Google | this one should fail, but currently doesn't |
+----+----------+----------+--------+---------------------------------------------+
Подробнее здесь: https://stackoverflow.com/questions/576 ... constraint
Мобильная версия