Ejemplo n.º 1
0
 class Meta:
     db_table = "post"
     constraints = [
         CheckConstraint(
             name="has_subject_iff_is_thread",
             check=(
                 (Q(parent_thread__isnull=True) & ~Q(subject=""))
                 | Q(parent_thread__isnull=False, subject="")
             ),
         ),
         CheckConstraint(
             name="has_board_iff_is_thread",
             check=(
                 Q(parent_thread__isnull=True, board__isnull=False)
                 | Q(parent_thread__isnull=False, board__isnull=True)
             ),
         ),
         CheckConstraint(
             name="has_pic_if_is_thread",
             check=(
                 Q(parent_thread__isnull=True, pic__isnull=False)
                 | Q(parent_thread__isnull=False)
             ),
         ),
     ]
Ejemplo n.º 2
0
 class Meta:
     managed = True
     db_table = 'purchase'
     verbose_name_plural = 'purchase'
     constraints = [
         CheckConstraint(check=Q(price_gte=0), name='price_chk_2'),
         CheckConstraint(check=Q(quantity_gte=0), name='quantity_chk_2')
     ]
Ejemplo n.º 3
0
 class Meta:
     app_label = 'fruit_app'
     unique_together = (('country', 'commodity'), )
     constraints = [
         CheckConstraint(check=Q(fixed_overhead__gte=0),
                         name='fixed_overhead_not_negative'),
         CheckConstraint(check=Q(variable_cost__gte=0),
                         name='variable_cost_not_negative'),
     ]
Ejemplo n.º 4
0
    class Meta:
        """The metaclass contains database constraints"""

        app_label = 'fruit_app'

        constraints = [
            CheckConstraint(check=Q(country_code__regex=r'^[a-zA-Z]{2,2}'),
                            name='country_2_letter_code'),
            CheckConstraint(check=Q(name__regex=r'^[a-zA-Z\s]+$'),
                            name='country_name'),
        ]
Ejemplo n.º 5
0
 class Meta:
     managed = True
     db_table = 'inventory'
     verbose_name_plural = 'inventory'
     constraints = [
         CheckConstraint(check=Q(quantity_gte=0), name='quantity_chk_1')
     ]
Ejemplo n.º 6
0
 class Meta:
     managed = True
     db_table = 'sales'
     verbose_name_plural = 'sales'
     constraints = [
         CheckConstraint(check=Q(quantity_gte=0), name='quantity_chk_3')
     ]
Ejemplo n.º 7
0
 class Meta:
     managed = True
     db_table = 'Food'
     verbose_name_plural = 'Food'
     constraints = [
         CheckConstraint(check=Q(price_gte=0), name='price_chk_1')
     ]
 def test_check_constraint_datetimerange_contains(self):
     constraint_name = 'timestamps_contains'
     self.assertNotIn(constraint_name,
                      self.get_constraints(RangesModel._meta.db_table))
     constraint = CheckConstraint(
         check=Q(timestamps__contains=F('timestamps_inner')),
         name=constraint_name,
     )
     with connection.schema_editor() as editor:
         editor.add_constraint(RangesModel, constraint)
     with connection.cursor() as cursor:
         constraints = connection.introspection.get_constraints(
             cursor, RangesModel._meta.db_table)
     self.assertIn(constraint_name, constraints)
     datetime_1 = datetime.datetime(2016, 1, 1)
     datetime_2 = datetime.datetime(2016, 1, 2, 12)
     with self.assertRaises(IntegrityError), transaction.atomic():
         RangesModel.objects.create(
             timestamps=(datetime_1, datetime_2),
             timestamps_inner=(datetime_1, datetime_2.replace(hour=13)),
         )
     RangesModel.objects.create(
         timestamps=(datetime_1, datetime_2),
         timestamps_inner=(datetime_1, datetime_2),
     )
Ejemplo n.º 9
0
 class Meta:
     constraints = [
         CheckConstraint(
             check=Q(max_atendees_count__gte=5) & Q(
                 max_atendees_count__lte=10),
             name='max_atendees_count_between_5_and_10'
         )
     ]
Ejemplo n.º 10
0
 class Meta:
     verbose_name = 'usuario'
     verbose_name_plural = 'usuarios'
     swappable = 'AUTH_USER_MODEL'
     constraints = [
         CheckConstraint(check=(Q(can_approve=True) & Q(user_type=1)) |
                         (Q(can_approve=False) & Q(user_type__in=(1, 2))),
                         name='can_approve_only_employee'),
     ]
Ejemplo n.º 11
0
    class Meta:
        """Meta class."""

        # The goal is to ensure that exactly one field is not null.
        constraints = [
            # NOT (bucket IS NULL AND data_source IS NULL)
            CheckConstraint(
                check=~models.Q(models.Q(bucket=None) \
                                & models.Q(data_source={})),
                name='bucket_and_data_source_both_null'
            ),
            # NOT (bucket IS NOT NULL or '' AND data_source IS NOT NULL)
            CheckConstraint(
                check=~models.Q(~(models.Q(bucket=None) | models.Q(bucket='')) \
                                & ~models.Q(data_source={})),
                name='bucket_and_data_source_both_not_null'
            ),
        ]
Ejemplo n.º 12
0
    class Meta:
        """Meta class."""

        # The goal is to ensure that exactly one field is not null.
        constraints = [
            # NOT (provider_resource_name IS NULL AND credentials IS NULL)
            CheckConstraint(
                check=~models.Q(models.Q(provider_resource_name=None) \
                                & models.Q(credentials={})),
                name='credentials_and_resource_name_both_null'
            ),
            # NOT (provider_resource_name IS NOT NULL AND credentials IS NOT NULL)
            CheckConstraint(
                check=~models.Q(~models.Q(provider_resource_name=None) \
                                & ~models.Q(credentials={})),
                name='credentials_and_resource_name_both_not_null'
            ),
        ]
Ejemplo n.º 13
0
    class Meta:
        """Meta class."""

        # The goal is to ensure that exactly one field is not null.
        constraints = [
            # NOT (bucket IS NULL AND data_source IS NULL)
            CheckConstraint(
                check=~models.Q(models.Q(bucket=None) & models.Q(data_source={})),
                name="bucket_and_data_source_both_null",
            )
        ]
Ejemplo n.º 14
0
 class Meta:
     verbose_name = _("Condition")
     verbose_name_plural = _("Conditions")
     constraints = [
         CheckConstraint(
             check=(
                 (Q(named_condition__isnull=False) | Q(conds__isnull=False)) &
                 (Q(named_condition__isnull=True) | Q(conds__isnull=True))
             ),
             name="conds_xor_named_condition_is_null",
         ),
     ]
Ejemplo n.º 15
0
    class Meta:
        constraints = [
            # Either internal or external must be set and not both
            CheckConstraint(
                check=Q(internal__isnull=False, external__isnull=True) | Q(internal__isnull=True, external__isnull=False),
                name='either_internal_or_external'
            ),

            # Internal must be unique if it is not null
            UniqueConstraint(fields=['internal'], condition=Q(internal__isnull=False), name='unique_internal'),

            # External must be unique if it is not null
            UniqueConstraint(fields=['external'], condition=Q(external__isnull=False), name='unique_external'),
        ]
Ejemplo n.º 16
0
 def test_check_constraint_range_value(self):
     constraint_name = 'ints_between'
     self.assertNotIn(constraint_name,
                      self.get_constraints(RangesModel._meta.db_table))
     constraint = CheckConstraint(
         check=Q(ints__contained_by=NumericRange(10, 30)),
         name=constraint_name,
     )
     with connection.schema_editor() as editor:
         editor.add_constraint(RangesModel, constraint)
     self.assertIn(constraint_name,
                   self.get_constraints(RangesModel._meta.db_table))
     with self.assertRaises(IntegrityError), transaction.atomic():
         RangesModel.objects.create(ints=(20, 50))
     RangesModel.objects.create(ints=(10, 30))
Ejemplo n.º 17
0
 class Meta:
     verbose_name = _("Lesson documentation")
     verbose_name_plural = _("Lesson documentations")
     ordering = [
         "year",
         "week",
         "lesson_period__period__weekday",
         "lesson_period__period__period",
     ]
     constraints = [
         CheckConstraint(
             check=lesson_related_constraint_q,
             name="one_relation_only_lesson_documentation",
         ),
         models.UniqueConstraint(
             fields=("lesson_period", "week", "year", "event",
                     "extra_lesson"),
             name="unique_documentation_per_object",
         ),
     ]
Ejemplo n.º 18
0
 class Meta:
     verbose_name = _("Personal note")
     verbose_name_plural = _("Personal notes")
     ordering = [
         "year",
         "week",
         "lesson_period__period__weekday",
         "lesson_period__period__period",
         "person__last_name",
         "person__first_name",
     ]
     constraints = [
         CheckConstraint(check=lesson_related_constraint_q,
                         name="one_relation_only_personal_note"),
         models.UniqueConstraint(
             fields=("lesson_period", "week", "year", "event",
                     "extra_lesson"),
             name="unique_personal_note_per_object",
         ),
     ]
Ejemplo n.º 19
0
 def test_check_constraint_daterange_contains(self):
     constraint_name = 'dates_contains'
     self.assertNotIn(constraint_name,
                      self.get_constraints(RangesModel._meta.db_table))
     constraint = CheckConstraint(
         check=Q(dates__contains=F('dates_inner')),
         name=constraint_name,
     )
     with connection.schema_editor() as editor:
         editor.add_constraint(RangesModel, constraint)
     self.assertIn(constraint_name,
                   self.get_constraints(RangesModel._meta.db_table))
     date_1 = datetime.date(2016, 1, 1)
     date_2 = datetime.date(2016, 1, 4)
     with self.assertRaises(IntegrityError), transaction.atomic():
         RangesModel.objects.create(
             dates=(date_1, date_2),
             dates_inner=(date_1, date_2.replace(day=5)),
         )
     RangesModel.objects.create(
         dates=(date_1, date_2),
         dates_inner=(date_1, date_2),
     )
Ejemplo n.º 20
0
 class Meta:
     constraints = (CheckConstraint(check=Q(user__isnull=True)
                                    | Q(_card_number__isnull=True),
                                    name="user_or_cardnumber_null"), )
Ejemplo n.º 21
0
 class Meta:
     constraints = (CheckConstraint(check=Q(user__isnull=True)
                                    | Q(_card_number__isnull=True),
                                    name="user_or_cardnumber_null"), )
     verbose_name = _("3D printer course")
     verbose_name_plural = _("3D printer courses")
Ejemplo n.º 22
0
 class Meta:
     unique_together = ("room", "timeslot", "conference")
     constraints = [
         CheckConstraint(check=Q(start_time__lte=F('end_time')),
                         name="valid-time")
     ]