ValueError: The database backend does not accept 0 as a value for AutoField.

2,548 views
Skip to first unread message

Hamza Mirchi

unread,
Apr 6, 2020, 9:35:22 AM4/6/20
to Django users
Hi Everyone,

I'm Saving My Mail Model but i can't but i get that error again and again

This is my model:

class Mail(models.Model):
 choices
= (('Valid','Valid'),('InValid','InValid'),('Unknown','Unknown'),('Extracted','Extracted',))
 providers
= (('Gmail','Gmail'),('Yahoo','Yahoo'),('Hotmail','Hotmail'),('Outlook','Outlook'),('Live','Live'),('Aol','Aol'),('Protonmail','Protonmail'),('Other','Other'),)
 user
= models.ForeignKey(User,on_delete=models.CASCADE,blank=True,null=True)
 email
= models.EmailField(blank=False,null=False)
 status
= models.CharField(max_length=100,choices=choices)
 type
= models.CharField(max_length=100,choices=providers)


 
def __str__(self):
 
return self.email



This is my Code:

mail = Mail.objects.create(user=request.user,email=str(g),status='Valid',type='Gmail')
mail
.save()


Here is The Error:


Traceback (most recent call last):
 
File "/usr/lib/python3.7/multiprocessing/process.py", line 297, in _bootstrap
   
self.run()
 
File "/usr/lib/python3.7/multiprocessing/process.py", line 99, in run
   
self._target(*self._args, **self._kwargs)
 
File "/media/lenovo/Local Disk/Learning/Scripts/Django/emailv3/src/validator/views.py", line 703, in g6
    mail
.save()
 
File "/home/lenovo/.local/lib/python3.7/site-packages/django/db/models/base.py", line 746, in save
    force_update
=force_update, update_fields=update_fields)
 
File "/home/lenovo/.local/lib/python3.7/site-packages/django/db/models/base.py", line 784, in save_base
    force_update
, using, update_fields,
 
File "/home/lenovo/.local/lib/python3.7/site-packages/django/db/models/base.py", line 887, in _save_table
    results
= self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
 
File "/home/lenovo/.local/lib/python3.7/site-packages/django/db/models/base.py", line 926, in _do_insert
   
using=using, raw=raw,
 
File "/home/lenovo/.local/lib/python3.7/site-packages/django/db/models/manager.py", line 82, in manager_method
   
return getattr(self.get_queryset(), name)(*args, **kwargs)
 
File "/home/lenovo/.local/lib/python3.7/site-packages/django/db/models/query.py", line 1204, in _insert
   
return query.get_compiler(using=using).execute_sql(returning_fields)
 
File "/home/lenovo/.local/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1390, in execute_sql
   
for sql, params in self.as_sql():
 
File "/home/lenovo/.local/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1335, in as_sql
   
for obj in self.query.objs
 
File "/home/lenovo/.local/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1335, in <listcomp>
   
for obj in self.query.objs
 
File "/home/lenovo/.local/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1334, in <listcomp>
   
[self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
 
File "/home/lenovo/.local/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1275, in prepare_value
    value
= field.get_db_prep_save(value, connection=self.connection)
 
File "/home/lenovo/.local/lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 821, in get_db_prep_save
   
return self.get_db_prep_value(value, connection=connection, prepared=False)
 
File "/home/lenovo/.local/lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 2366, in get_db_prep_value
    value
= connection.ops.validate_autopk_value(value)
 
File "/home/lenovo/.local/lib/python3.7/site-packages/django/db/backends/mysql/operations.py", line 192, in validate_autopk_value
   
raise ValueError('The database backend does not accept 0 as a '
ValueError: The database backend does not accept 0 as a value for AutoField




Screenshot from 2020-04-06 06-38-10.png

Simon Charette

unread,
Apr 6, 2020, 10:15:42 AM4/6/20
to Django users
Hello there,

By default MySQL interprets providing 0 for an AUTO_INCREMENT PRIMARY KEY as NULL which defaults to increment the field.

CREATE TABLE foo (id int auto_increment primary key);
INSERT INTO foo (id) VALUES(0);
INSERT INTO foo (id) VALUES(0);
SELECT * FROM foo;
+----+
| id |
+----+
| 1  |
| 2  |
+----+

This ORM level check is to prevent you from shooting yourself in the foot by expecting that providing a zero will assign this value.

Cheers,
Simon

Hamza Mirchi

unread,
Apr 6, 2020, 11:51:46 AM4/6/20
to django...@googlegroups.com
How Can I Fix That Via Django?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/86f4aedc-2241-4ca9-b088-afb511d1733a%40googlegroups.com.

Simon Charette

unread,
Apr 6, 2020, 1:26:39 PM4/6/20
to Django users
Something in your code is assigning a id=0 to Mail before calling .save() on it.

If you identify its origin then removing it should address your problem.

Simon

Le lundi 6 avril 2020 11:51:46 UTC-4, Hamza Mirchi a écrit :
How Can I Fix That Via Django?

To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages