UniqueConstraint connection with function

SQLAlchemy quick query ...

I have a class "Document" with the attributes "Number" and "Date". I need to ensure that there is no duplicate number during the same year, is there a way to have UniqueConstraint on "Number + year (Date)"? Should I use a unique index instead? How to declare a functional part?

(SQLAlchemy 0.5.5, PostgreSQL 8.3.4)

Thanks in advance!

+3
source share
2 answers

. , SQLAlchemy . DDL . , :

DDL(
    "CREATE UNIQUE INDEX doc_year_num_uniq ON %(fullname)s "
    "(EXTRACT(YEAR FROM date), number)"
).execute_at('after-create', Document.__table__)

, SADeprecation v0.7 , :

from sqlalchemy import event

event.listen(ModelObject.__table__,
         'after_create',
          DDL("CREATE UNIQUE INDEX term_year ON %(fullname)s "
              "(EXTRACT(YEAR FROM start_date), term)",
              on = 'postgresql'
              )
         )
+3

, , , . , , year date, number. , , date , , . .

0

Source: https://habr.com/ru/post/1719106/


All Articles