简体   繁体   中英

IntegrityError: (1048, “Column 'username' cannot be null”) when emailid is also unique key

here is my custom User model having email id as unique key(along with username).

class User(AbstractUser):
    # some more other fields
    class Meta:
        unique_together = ('email', )

Now I've created custom backend mentioned here (for email auth) Django authentication with custom user model with email-id as unique key

till now everything looks fine, working!I'm able to Login, Logout, etc

Now I have two rows/objects, one for superuser, which has username and emailid both, second row has only emailid but username is Null(that i created from shell).

now when I try to create a User object with some unique email-id it shows integrity error(mentioned in question title).

I used this query to create a new user

u, c = User.objects.get_or_create(email='some-new-unique-email@gmail.com')

according to Unique key definition, it can accepts multiple Null value, right?

what is wrong with my implementation. any help would be appreciated.

here is table representation, both email and username is UNI and NOTNULL

mysql> describe myapp_user;
+--------------+---------------+------+-----+---------+----------------+
| Field        | Type          | Null | Key | Default | Extra          |
+--------------+---------------+------+-----+---------+----------------+
| id           | int(11)       | NO   | PRI | NULL    | auto_increment |
| password     | varchar(128)  | NO   |     | NULL    |                |
| last_login   | datetime      | YES  |     | NULL    |                |
| is_superuser | tinyint(1)    | NO   |     | NULL    |                |
| username     | varchar(30)   | NO   | UNI | NULL    |                |
| first_name   | varchar(30)   | NO   |     | NULL    |                |
| last_name    | varchar(30)   | NO   |     | NULL    |                |
| email        | varchar(254)  | NO   | UNI | NULL    |                |
| is_staff     | tinyint(1)    | NO   |     | NULL    |                |
| is_active    | tinyint(1)    | NO   |     | NULL    |                |
| date_joined  | datetime      | NO   |     | NULL    |                |

EDIT:

but I'm still surprised that 2nd entry in this table has no username value(checked twice, repr, bool etc.). if this is NOTNULL column how it is possible.

here is the output from django shell- (1st user, root is superuser and 2nd without username.

In [4]: User.objects.all()
Out[4]: [<User: root>, <User: >]

In [5]: User.objects.all().values_list('id', flat=True)
Out[5]: [1L, 2L]

In [6]: User.objects.get(id=2)
Out[6]: <User: >

In [7]: u = User.objects.get(id=2)

In [8]: u.__dict__
Out[8]: 
{'_state': <django.db.models.base.ModelState at 0x7f855c325d10>,
 'date_joined': datetime.datetime(2015, 8, 11, 7, 37, 42, tzinfo=<UTC>),
 'email': u'abc@gmail.com',
 'first_name': u'',
 'id': 2L,
 'is_active': True,
 'is_staff': False,
 'is_superuser': False,
 'last_login': datetime.datetime(2015, 8, 11, 11, 19, 50, tzinfo=<UTC>),
 'last_name': u'',
 'password': u'pbkdf2_sha256$20000$ylufrgAOMJYI$z8kc2pSckWTn3AHbFgzkP5AwGR9qh8+Mtd/pw3srxQE=',
 'username': u''}  

In [9]: repr(u.username)
Out[9]: "u''"

In [10]: type(u.username)
Out[10]: unicode

In [11]: bool(u.username)
Out[11]: False    

In [12]: str(u.username)
Out[12]: ''

PS: I just noticed that, first_name and last name is also NOTNULL but they are empty in both rows.

Basically Django User (or AbstractUser) model has username as a NOT NULL constraint. So, by adding this constraint, you are just adding the unique constraint for email.

This still has username is NOT NULL constraint. You can configure the username to have the email address itself, like

username = email.split('@')[0]

at the MyModel.objects.create statement

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM