Contrainte unicité dept_acronym

This commit is contained in:
Emmanuel Viennet 2022-08-04 16:10:11 +02:00
parent 21604259a4
commit 0b553a98a4
2 changed files with 35 additions and 2 deletions

View File

@ -6,6 +6,7 @@ from typing import Any
from app import db
from app.models import SHORT_STR_LEN
from app.scodoc.sco_exceptions import ScoValueError
class Departement(db.Model):
@ -13,7 +14,7 @@ class Departement(db.Model):
id = db.Column(db.Integer, primary_key=True)
acronym = db.Column(
db.String(SHORT_STR_LEN), nullable=False, index=True
db.String(SHORT_STR_LEN), nullable=False, index=True, unique=True
) # ne change jamais, voir la pref. DeptName
description = db.Column(db.Text()) # pas utilisé par ScoDoc : voir DeptFullName
date_creation = db.Column(db.DateTime(timezone=True), server_default=db.func.now())
@ -60,7 +61,7 @@ def create_dept(acronym: str, visible=True) -> Departement:
existing = Departement.query.filter_by(acronym=acronym).count()
if existing:
raise ValueError(f"acronyme {acronym} déjà existant")
raise ScoValueError(f"acronyme {acronym} déjà existant")
departement = Departement(acronym=acronym, visible=visible)
p1 = ScoPreference(name="DeptName", value=acronym, departement=departement)
db.session.add(p1)

View File

@ -0,0 +1,32 @@
"""unicite dept_acronym
Revision ID: 814e8e37d082
Revises: 57179ae34069
Create Date: 2022-08-04 16:07:59.710487
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = "814e8e37d082"
down_revision = "57179ae34069"
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index("ix_departement_acronym", table_name="departement")
op.create_index(
op.f("ix_departement_acronym"), "departement", ["acronym"], unique=True
)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f("ix_departement_acronym"), table_name="departement")
op.create_index("ix_departement_acronym", "departement", ["acronym"], unique=False)
# ### end Alembic commands ###