Bladeren bron

Enable unique constraints for unique fields

Maarten van den Berg 5 jaren geleden
bovenliggende
commit
f75bafe9f7
2 gewijzigde bestanden met toevoegingen van 40 en 4 verwijderingen
  1. 36 0
      piket_server/alembic/versions/6a5989118ee3_enable_unique_constraints.py
  2. 4 4
      piket_server/models.py

+ 36 - 0
piket_server/alembic/versions/6a5989118ee3_enable_unique_constraints.py

@@ -0,0 +1,36 @@
1
+"""Enable unique constraints
2
+
3
+Revision ID: 6a5989118ee3
4
+Revises: cca57457a0a6
5
+Create Date: 2019-09-22 17:04:01.945713
6
+
7
+"""
8
+from alembic import op
9
+import sqlalchemy as sa
10
+
11
+
12
+# revision identifiers, used by Alembic.
13
+revision = "6a5989118ee3"
14
+down_revision = "cca57457a0a6"
15
+branch_labels = None
16
+depends_on = None
17
+
18
+
19
+def upgrade():
20
+    with op.batch_alter_table("consumption_types") as batch_op:
21
+        batch_op.create_unique_constraint("uc_consumption_types_name", ["name"])
22
+
23
+    with op.batch_alter_table("people") as batch_op2:
24
+        batch_op2.create_unique_constraint("uc_people_aardbei_id", ["aardbei_id"])
25
+        batch_op2.create_unique_constraint("uc_people_full_name", ["full_name"])
26
+        batch_op2.create_unique_constraint("uc_people_display_name", ["display_name"])
27
+
28
+
29
+def downgrade():
30
+    with op.batch_alter_table("people") as batch_op2:
31
+        batch_op2.drop_constraint("uc_people_display_name", type_="unique")
32
+        batch_op2.drop_constraint("uc_people_full_name", type_="unique")
33
+        batch_op2.drop_constraint("uc_people_aardbei_id", type_="unique")
34
+
35
+    with op.batch_alter_table("consumption_types") as batch_op:
36
+        batch_op.drop_constraint("uc_consumption_types_name", type_="unique")

+ 4 - 4
piket_server/models.py

@@ -18,9 +18,9 @@ class Person(db.Model):
18 18
     __tablename__ = "people"
19 19
 
20 20
     person_id = db.Column(db.Integer, primary_key=True)
21
-    full_name = db.Column(db.String, nullable=False)
22
-    display_name = db.Column(db.String, nullable=True)
23
-    aardbei_id = db.Column(db.Integer, nullable=True)
21
+    full_name = db.Column(db.String, nullable=False, unique=True)
22
+    display_name = db.Column(db.String, nullable=True, unique=True)
23
+    aardbei_id = db.Column(db.Integer, nullable=True, unique=True)
24 24
     active = db.Column(db.Boolean, nullable=False, default=False)
25 25
 
26 26
     consumptions = db.relationship("Consumption", backref="person", lazy=True)
@@ -194,7 +194,7 @@ class ConsumptionType(db.Model):
194 194
     __tablename__ = "consumption_types"
195 195
 
196 196
     consumption_type_id = db.Column(db.Integer, primary_key=True)
197
-    name = db.Column(db.String, nullable=False)
197
+    name = db.Column(db.String, nullable=False, unique=True)
198 198
     icon = db.Column(db.String)
199 199
     active = db.Column(db.Boolean, default=True)
200 200