diff --git a/app/models/formsemestre.py b/app/models/formsemestre.py index 89468ab2f5..93b781b7d4 100644 --- a/app/models/formsemestre.py +++ b/app/models/formsemestre.py @@ -273,6 +273,7 @@ class NotesModuleImplInscription(db.Model): """Inscription à un module (etudiants,moduleimpl)""" __tablename__ = "notes_moduleimpl_inscription" + __table_args__ = (db.UniqueConstraint("moduleimpl_id", "etudid"),) id = db.Column(db.Integer, primary_key=True) moduleimpl_inscription_id = db.synonym("id") diff --git a/migrations/versions/d74b4e16fb3c_scodoc_9_0_51_add_unicity_constraint_on_.py b/migrations/versions/d74b4e16fb3c_scodoc_9_0_51_add_unicity_constraint_on_.py new file mode 100644 index 0000000000..1c9da8a4e6 --- /dev/null +++ b/migrations/versions/d74b4e16fb3c_scodoc_9_0_51_add_unicity_constraint_on_.py @@ -0,0 +1,46 @@ +"""ScoDoc 9.0.51: add unicity constraint on notes_moduleimpl_inscription + +Revision ID: d74b4e16fb3c +Revises: f86c013c9fbd +Create Date: 2021-10-09 20:08:50.927330 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.orm import sessionmaker # added by ev + + +# revision identifiers, used by Alembic. +revision = "d74b4e16fb3c" +down_revision = "f86c013c9fbd" +branch_labels = None +depends_on = None + +Session = sessionmaker() + + +def upgrade(): + # Added by ev: remove duplicates + bind = op.get_bind() + session = Session(bind=bind) + session.execute( + """ + DELETE FROM notes_moduleimpl_inscription i1 + USING notes_moduleimpl_inscription i2 + WHERE i1.id < i2.id + AND i1.moduleimpl_id = i2.moduleimpl_id + AND i1.etudid = i2.etudid; + """ + ) + + # ### commands auto generated by Alembic - please adjust! ### + op.create_unique_constraint( + None, "notes_moduleimpl_inscription", ["moduleimpl_id", "etudid"] + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_constraint(None, "notes_moduleimpl_inscription", type_="unique") + # ### end Alembic commands ###