The error message you’re encountering suggests that the id field in your Validation model is not being set correctly when you try to create a new instance. Since you’ve specified that the id field is a BigAutoField, it should auto-increment and not require a manual value.
Here are some things to check and consider:
Review the Validation Model Your Validation model appears to be set up correctly to auto-generate an id. However, if you have previously altered the table schema directly in the database or if there were issues with migrations, it could lead to unexpected behavior. Make sure your database schema aligns with your model definitions.
Database Migration Ensure that your migrations are properly applied. Sometimes, after altering models, you may need to recreate your migrations. Run the following commands:
bash code: python manage.py makemigrations python manage.py migrate
SQL code: SELECT setval('validation_id_seq', (SELECT MAX(id) FROM validation));
Make sure the sequence name is correct. If validation_id_seq is not the correct name, you can find the actual sequence name using the following query:
SQL code: SELECT * FROM pg_sequences WHERE schemaname = 'public';
python code: Validation.objects.create(user=user, token=token, expired=timezone.now() + timezone.timedelta(hours=1))
This code should work fine if the Validation model is set up correctly. If you are still encountering issues, consider trying this alternative way of creating the object:
python code: validation_instance = Validation(user=user, token=token, expired=timezone.now() + timezone.timedelta(hours=1)) validation_instance.save()
Check the Database Directly If issues persist, connect to your database using pgAdmin and inspect the validation table structure. Ensure that the id column is defined as a BigSerial (or equivalent) type, which auto-increments.
Clean the Database (if necessary) If the validation table has data that conflicts with your model definition, and if it's feasible, you may want to delete any conflicting rows or even drop the table and recreate it. Note: This is destructive, so make sure you have backups or are working in a development environment.