Opened 14 years ago

Closed 13 years ago

#14292 closed Uncategorized (worksforme)

User.objects.create_user does not give object with id field populated in svn release.

Reported by: Chris Young <chris.young@…> Owned by: nobody
Component: contrib.auth Version: dev
Severity: Normal Keywords: create_user
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

See line below starting with ### for expected behavior vs. actual.

I'm using the following svn release of Django:

URL: http://code.djangoproject.com/svn/django/trunk
Repository Root: http://code.djangoproject.com/svn
Repository UUID: bcc190cf-cafb-0310-a4f2-bffc1f526a37
Revision: 13858
Node Kind: directory
Schedule: normal
Last Changed Author: lukeplant
Last Changed Rev: 13857
Last Changed Date: 2010-09-15 05:40:23 +0800 (Wed, 15 Sep 2010)

I'm using Python 2.4.3 (#1, Sep 3 2009, 15:37:37).
I'm using Django with the postgresql_psycopg2 backend.

On 1.2.3 the behavior is as expected:

>>> import django
>>> django.VERSION
(1, 2, 3, 'final', 0)
>>> from django.contrib.auth.models import User
>>> x = User.objects.get(username='foo')
>>> x.delete()
>>> user = User.objects.create_user('foo', 'bar@bar.com', 'baz')
>>> user.id
323L
>>> user.save()
>>>

On the svn release, same server, I do not get an id back:

>>> import django
>>> django.VERSION
(1, 3, 0, 'alpha', 0)
>>> from django.contrib.auth.models import User
>>> x = User.objects.get(username='foo')
>>> x.delete()
>>> user = User.objects.create_user('foo', 'bar@bar.com', 'baz')
>>> user.id
>>>

### I expect the integer user.id to be returned above, instead it is None.
At this point, the entry is actually in the database again, but If I try to save, I get the further error here because it appears to try to INSERT it again.

>>> user.save()
Traceback (most recent call last):
  File "<console>", line 1, in ?
  File "/usr/lib/python2.4/site-packages/django/db/models/base.py", line 434, in save
    self.save_base(using=using, force_insert=force_insert, force_update=force_update)
  File "/usr/lib/python2.4/site-packages/django/db/models/base.py", line 527, in save_base
    result = manager._insert(values, return_id=update_pk, using=using)
  File "/usr/lib/python2.4/site-packages/django/db/models/manager.py", line 195, in _insert
    return insert_query(self.model, values, **kwargs)
  File "/usr/lib/python2.4/site-packages/django/db/models/query.py", line 1480, in insert_query
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/usr/lib/python2.4/site-packages/django/db/models/sql/compiler.py", line 783, in execute_sql
    cursor = super(SQLInsertCompiler, self).execute_sql(None)
  File "/usr/lib/python2.4/site-packages/django/db/models/sql/compiler.py", line 727, in execute_sql
    cursor.execute(sql, params)
  File "/usr/lib/python2.4/site-packages/django/db/backends/util.py", line 15, in execute
    return self.cursor.execute(sql, params)
  File "/usr/lib/python2.4/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 44, in execute
    return self.cursor.execute(query, args)
IntegrityError: duplicate key value violates unique constraint "auth_user_username_key"

Change History (7)

comment:1 by Jardel Weyrich, 14 years ago

Just FWIW, django.contrib.auth.models.User hasn't changed since 1.2.0b1, so the bug is probably on the underlying save mechanism, and not on the create_user method itself.

comment:2 by Luke Plant, 14 years ago

Resolution: worksforme
Status: newclosed

I have tried with:

  • Python 2.4.6 on Ubuntu 10.04, with Postgres 8.4
  • Python 2.4.3 on CentOS release 5.5, with Postgres 8.3
  • More recent versions of Python.

and cannot reproduce this. So I'm closing as 'worksforme'. If you can provide more details that would enable us to track this down, please re-open.

Thanks.

comment:3 by Jacob, 13 years ago

milestone: 1.3

Milestone 1.3 deleted

comment:4 by anonymous, 13 years ago

Easy pickings: unset
Resolution: worksforme
Severity: Normal
Status: closedreopened
Type: Uncategorized
UI/UX: unset

Python 2.7.2 on Mac OS X 10.7.2:

python manage.py syncdb --noinput
python manage.py createsuperuser --noinput --email=admin@example.com --username=admin
python << EOF
from os import environ
environ['DJANGO_SETTINGS_MODULE']='settings'
from django.contrib.auth.models import User
from django.contrib.auth import authenticate
password=open('.django.admin.password').read().strip()
if authenticate(username='admin', password=password) is None:
    u = User.objects.get(username__exact='admin')
    u.set_password(password)
    u.save()
    print "Admin test login successful (password saved encrypted in db, plain text in .django.admin.password)."
else:
    print "Admin test login successful (password already set, plain text in .django.admin.password)."
EOF
Creating tables ...
Installing custom SQL ...
Installing indexes ...
No fixtures found.
Traceback (most recent call last):
  File "manage.py", line 14, in <module>
    execute_manager(settings)
  File "/run/.virtualenvs/django_template/lib/python2.7/site-packages/django/core/management/__init__.py", line 438, in execute_manager
    utility.execute()
  File "/run/.virtualenvs/django_template/lib/python2.7/site-packages/django/core/management/__init__.py", line 379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/run/.virtualenvs/django_template/lib/python2.7/site-packages/django/core/management/base.py", line 191, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/run/.virtualenvs/django_template/lib/python2.7/site-packages/django/core/management/base.py", line 220, in execute
    output = self.handle(*args, **options)
  File "/run/.virtualenvs/django_template/lib/python2.7/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 134, in handle
    User.objects.create_superuser(username, email, password)
  File "/run/.virtualenvs/django_template/lib/python2.7/site-packages/django/contrib/auth/models.py", line 140, in create_superuser
    u = self.create_user(username, email, password)
  File "/run/.virtualenvs/django_template/lib/python2.7/site-packages/django/contrib/auth/models.py", line 136, in create_user
    user.save(using=self._db)
  File "/run/.virtualenvs/django_template/lib/python2.7/site-packages/django/db/models/base.py", line 460, in save
    self.save_base(using=using, force_insert=force_insert, force_update=force_update)
  File "/run/.virtualenvs/django_template/lib/python2.7/site-packages/django/db/models/base.py", line 553, in save_base
    result = manager._insert(values, return_id=update_pk, using=using)
  File "/run/.virtualenvs/django_template/lib/python2.7/site-packages/django/db/models/manager.py", line 195, in _insert
    return insert_query(self.model, values, **kwargs)
  File "/run/.virtualenvs/django_template/lib/python2.7/site-packages/django/db/models/query.py", line 1436, in insert_query
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/run/.virtualenvs/django_template/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 791, in execute_sql
    cursor = super(SQLInsertCompiler, self).execute_sql(None)
  File "/run/.virtualenvs/django_template/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 735, in execute_sql
    cursor.execute(sql, params)
  File "/run/.virtualenvs/django_template/lib/python2.7/site-packages/django/db/backends/util.py", line 34, in execute
    return self.cursor.execute(sql, params)
  File "/run/.virtualenvs/django_template/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 44, in execute
    return self.cursor.execute(query, args)
django.db.utils.IntegrityError: duplicate key value violates unique constraint "auth_user_username_key"
DETAIL:  Key (username)=(admin) already exists.

comment:5 by anonymous, 13 years ago

Django 1.3, psycopg2 2.4.2, Postgres 9.1.1

comment:6 by anonymous, 13 years ago

2011-10-27 20:22:02 PDT ERROR   duplicate key value violates unique constraint "auth_user_username_key"
2011-10-27 20:22:02 PDT DETAIL  Key (username)=(admin) already exists.  
2011-10-27 20:22:02 PDT STATEMENT       INSERT INTO "auth_user" ("username", "first_name", "last_name", "email", "password", "is_staff", "is_active", "is_superuser", "last_login", "date_joined") VALUES ('admin', '', '', 'admin@example.com', '!', false, true, false, '2011-10-27 20:22:02.438658', '2011-10-27 20:22:02.438658')

Obviously, the error is on createsuperuser attempting to create the same user again.

comment:7 by anonymous, 13 years ago

Resolution: worksforme
Status: reopenedclosed

Very simply hack:
append >/dev/null 2>&1 || true
to createsuperuser to ensure the script is idempotent.

Note: See TracTickets for help on using tickets.
Back to Top