Stack Overflow Asked on January 5, 2022
I have this model:
class TestopiaEvent(Model):
event_id = AutoField(primary_key=True)
name = CharField(max_length=255)
start_date = DateField()
end_date = DateField()
testers_required = IntegerField()
class Meta:
constraints = [
CheckConstraint(
check=Q(start_date__lte=F('end_date'), start_date__gte=datetime.now().date()),
name='correct_datetime'
)
]
And this test:
class TestopiaEventTestCase(TestCase):
def setUp(self):
self.default_values = {
'name': 'Testopia 1',
'start_date': datetime.now().date(),
'end_date': datetime.now().date() + timedelta(days=1),
'testers_required': 1
}
self.testopia_event = TestopiaEvent(**self.default_values)
def test_save_with_valid_model_check_database(self):
self.assertIsNone(self.testopia_event.save())
And it fails with this error:
django.db.utils.IntegrityError: new row for relation "webserver_testopiaevent" violates check constraint "correct_datetime"
DETAIL: Failing row contains (1, Testopia 1, 2020-07-24 00:00:00+00, 2020-07-25 00:00:00+00, 1).
I don’t understand why it is failing as it should only fail if today’s date is less than the start date and the start date or/and the start date is greater than the end date, which it isn’t?
What have I done wrong? Thanks
Edit: Here are the postgresdb constraints:
testopia=# d+ webserver_testopiaevent
Table
"public.webserver_testopiaevent"
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
------------------+------------------------+-----------+----------+-----------------------------------------------------------+----------+--------------+-------------
event_id | integer | | not null | nextval('webserver_testopiaevent_event_id_seq'::regclass) | plain | |
name | character varying(255) | | not null | | extended | |
start_date | date | | not null | | plain | |
end_date | date | | not null | | plain | |
testers_required | integer | | not null | | plain | |
Indexes:
"webserver_testopiaevent_pkey" PRIMARY KEY, btree (event_id)
Check constraints:
"correct_datetime" CHECK (start_date >= statement_timestamp() AND start_date <= end_date)
Access method: heap
Now() returns a DateTimeField() so with the timestamp addition it will be more than the current date if my DateField is set to the same date.
Answered by OultimoCoder on January 5, 2022
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP