[Django] #25304: Trying to create a super user before first migrate raise a database error

50 views
Skip to first unread message

Django

unread,
Aug 23, 2015, 4:08:45 PM8/23/15
to django-...@googlegroups.com
#25304: Trying to create a super user before first migrate raise a database error
--------------------------------------+--------------------
Reporter: mlorant | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: contrib.auth | Version: 1.8
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 1
Easy pickings: 1 | UI/UX: 0
--------------------------------------+--------------------
When creating a new project, you can sometimes forget to run `manage.py
migrate` before creating the initial superuser (especially if you don't
execute `runserver` before, which display a warning about migrations not
applied). The resulting error make sense, it can't access to auth_user,
since it does not exist yet:


{{{
$ django-admin.py startproject sample
$ cd sample/ && python manage.py createsuperuser
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
[...]
File
"/vagrant/django/django/contrib/auth/management/commands/createsuperuser.py",
line 85, in handle
default_username = get_default_username()
File "/vagrant/django/django/contrib/auth/management/__init__.py", line
189, in get_default_username
auth_app.User._default_manager.get(username=default_username)
[...]
File "/vagrant/django/django/db/backends/sqlite3/base.py", line 323, in
execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: auth_user
}}}

... but with a little try/except, it could be nicer and give a more
meaningful information:

{{{
$ python manage.py createsuperuser
CommandError: You must execute `manage.py migrate` once before creating a
super user
}}}

I have a patch ready (as I said, it just a try/except, see attachment)
but, if accepted, it would need unit tests to be complete.

--
Ticket URL: <https://code.djangoproject.com/ticket/25304>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Aug 23, 2015, 4:08:58 PM8/23/15
to django-...@googlegroups.com
#25304: Trying to create a super user before first migrate raise a database error
----------------------------------+----------------------------

Reporter: mlorant | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: contrib.auth | Version: 1.8
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Easy pickings: 1
UI/UX: 0 |
----------------------------------+----------------------------
Changes (by mlorant):

* Attachment "try-except-createsuperuser.diff" added.

Django

unread,
Aug 23, 2015, 4:47:20 PM8/23/15
to django-...@googlegroups.com
#25304: Trying to create a super user before first migrate raise a database error
-------------------------------------+-------------------------------------
Reporter: mlorant | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: contrib.auth | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by shaib):

* needs_better_patch: => 1
* needs_tests: => 0
* needs_docs: => 0


Comment:

Hi,

Thanks for filing this report and patch. I think the patch is not an
improvement, though:

* The claim that you must run "migrate" is not true, the table could be
created in other ways;
* Since the user model is swappable, many interesting things could be
different from default;
* I suspect other database backends will throw different errors here;
* I suspect the sqlite backend will throw an `OperationalError` in
different circumstances (e.g. the database file cannot be written to
because of permissions or non-existing directory or device full or
whatever)

The error message, in general, could be made friendlier, but the source
error must not be hidden. I think we could accept a similar modification
where the `except:` clause catches `DatabaseError`, prints a hint along
the lines of "Have you run initial migrations for this project?", and re-
raises the exception.

For a test, I think you should try to temporarily swap out the user model;
that should create the invalid code-vs-database state to trigger the
exception.

--
Ticket URL: <https://code.djangoproject.com/ticket/25304#comment:1>

Django

unread,
Aug 24, 2015, 7:25:51 AM8/24/15
to django-...@googlegroups.com
#25304: Trying to create a super user before first migrate raise a database error
-------------------------------------+-------------------------------------
Reporter: mlorant | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: contrib.auth | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by timgraham):

I don't see much advantage to this. In my opinion, a developer should be
able to understand what the lack of a database table means. There are
other management commands that access the database -- do they all need to
be adjusted too?

--
Ticket URL: <https://code.djangoproject.com/ticket/25304#comment:2>

Django

unread,
Aug 25, 2015, 5:13:32 AM8/25/15
to django-...@googlegroups.com
#25304: Trying to create a super user before first migrate raise a database error
-------------------------------------+-------------------------------------
Reporter: mlorant | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: contrib.auth | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by mlorant):

I agree with you, a developer should understand that a missing table on a
Django default command means the database has not been created yet.
However, I sometimes answer to question even more... easy *cough*... (like
"Why my page is not working and throw `name 'url' is not defined` in
urls.py").

After some thoughts, the idea to raise the exception again with a hint
seems more friendly for both usages (new Django users and skilled one),
but it is not urgent at all. Also, it needs to establish a list of
commands to improve...

--
Ticket URL: <https://code.djangoproject.com/ticket/25304#comment:3>

Django

unread,
Aug 25, 2015, 9:21:57 AM8/25/15
to django-...@googlegroups.com
#25304: Allow management commands to check if database migrations are applied
-------------------------------------+-------------------------------------
Reporter: mlorant | Owner: nobody
Type: New feature | Status: new
Component: Core (Management | Version: 1.8
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* needs_better_patch: 1 => 0
* component: contrib.auth => Core (Management commands)
* easy: 1 => 0
* has_patch: 1 => 0
* type: Cleanup/optimization => New feature
* stage: Unreviewed => Accepted


Comment:

I don't think the exception catching solution would work perfectly. For
example, there's no guarantee that the database exception is due to
missing tables and not some other bad query, etc. A more viable solution
might be to allow management commands to check if all database migrations
are applied and to output a warning if not, similar to what `runserver`
does. Accepting the ticket for investigation of that idea. #24484 is
similar.

--
Ticket URL: <https://code.djangoproject.com/ticket/25304#comment:4>

Django

unread,
Aug 26, 2015, 3:34:47 PM8/26/15
to django-...@googlegroups.com
#25304: Allow management commands to check if database migrations are applied
-------------------------------------+-------------------------------------
Reporter: mlorant | Owner: nobody

Type: New feature | Status: new
Component: Core (Management | Version: 1.8
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by shaib):

FWIW, +1

--
Ticket URL: <https://code.djangoproject.com/ticket/25304#comment:5>

Django

unread,
Nov 12, 2015, 6:23:55 PM11/12/15
to django-...@googlegroups.com
#25304: Allow management commands to check if database migrations are applied
-------------------------------------+-------------------------------------
Reporter: mlorant | Owner:
| MounirMesselmeni
Type: New feature | Status: assigned

Component: Core (Management | Version: 1.8
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by MounirMesselmeni):

* status: new => assigned
* owner: nobody => MounirMesselmeni


--
Ticket URL: <https://code.djangoproject.com/ticket/25304#comment:6>

Django

unread,
Nov 12, 2015, 6:24:59 PM11/12/15
to django-...@googlegroups.com
#25304: Allow management commands to check if database migrations are applied
-------------------------------------+-------------------------------------
Reporter: mlorant | Owner:
| MounirMesselmeni
Type: New feature | Status: assigned
Component: Core (Management | Version: 1.8
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by MounirMesselmeni):

* has_patch: 0 => 1


Old description:

New description:

https://github.com/django/django/pull/5643

--

--
Ticket URL: <https://code.djangoproject.com/ticket/25304#comment:7>

Django

unread,
Nov 12, 2015, 6:28:16 PM11/12/15
to django-...@googlegroups.com
#25304: Allow management commands to check if database migrations are applied
-------------------------------------+-------------------------------------
Reporter: mlorant | Owner:
| MounirMesselmeni
Type: New feature | Status: assigned
Component: Core (Management | Version: 1.8
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by MounirMesselmeni:

Old description:

> https://github.com/django/django/pull/5643

New description:

--

--
Ticket URL: <https://code.djangoproject.com/ticket/25304#comment:8>

Django

unread,
Nov 14, 2015, 8:07:34 AM11/14/15
to django-...@googlegroups.com
#25304: Allow management commands to check if database migrations are applied
-------------------------------------+-------------------------------------
Reporter: mlorant | Owner:
| MounirMesselmeni
Type: New feature | Status: assigned
Component: Core (Management | Version: 1.8
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* needs_better_patch: 0 => 1
* needs_docs: 0 => 1


Comment:

Tests aren't running on the [https://github.com/django/django/pull/5643
pull request] and docs are needed.

--
Ticket URL: <https://code.djangoproject.com/ticket/25304#comment:9>

Django

unread,
Nov 15, 2015, 11:42:01 AM11/15/15
to django-...@googlegroups.com
#25304: Allow management commands to check if database migrations are applied
-------------------------------------+-------------------------------------
Reporter: mlorant | Owner:
| MounirMesselmeni
Type: New feature | Status: assigned
Component: Core (Management | Version: 1.8
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by MounirMesselmeni):

Tests fixed and docs added

--
Ticket URL: <https://code.djangoproject.com/ticket/25304#comment:10>

Django

unread,
Nov 15, 2015, 6:30:57 PM11/15/15
to django-...@googlegroups.com
#25304: Allow management commands to check if database migrations are applied
-------------------------------------+-------------------------------------
Reporter: mlorant | Owner:
| MounirMesselmeni
Type: New feature | Status: assigned
Component: Core (Management | Version: 1.8
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* needs_better_patch: 1 => 0
* needs_docs: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/25304#comment:11>

Django

unread,
Dec 21, 2015, 9:28:21 AM12/21/15
to django-...@googlegroups.com
#25304: Allow management commands to check if database migrations are applied
-------------------------------------+-------------------------------------
Reporter: mlorant | Owner:
| MounirMesselmeni
Type: New feature | Status: assigned
Component: Core (Management | Version: 1.8
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* needs_tests: 0 => 1


Comment:

Could use an additional test as noted on the pull request.

--
Ticket URL: <https://code.djangoproject.com/ticket/25304#comment:12>

Django

unread,
Feb 12, 2016, 7:21:40 AM2/12/16
to django-...@googlegroups.com
#25304: Allow management commands to check if database migrations are applied
-------------------------------------+-------------------------------------
Reporter: mlorant | Owner:
| MounirMesselmeni
Type: New feature | Status: assigned
Component: Core (Management | Version: 1.8
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* needs_tests: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/25304#comment:13>

Django

unread,
Feb 12, 2016, 1:35:53 PM2/12/16
to django-...@googlegroups.com
#25304: Allow management commands to check if database migrations are applied
-------------------------------------+-------------------------------------
Reporter: mlorant | Owner:
| MounirMesselmeni
Type: New feature | Status: closed

Component: Core (Management | Version: 1.8
commands) |
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"50931dfa5310d5ae1c6e2852d05bf1e86700827f" 50931df]:
{{{
#!CommitTicketReference repository=""
revision="50931dfa5310d5ae1c6e2852d05bf1e86700827f"
Fixed #25304 -- Allowed management commands to check if migrations are
applied.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/25304#comment:14>

Django

unread,
Feb 15, 2016, 9:40:48 AM2/15/16
to django-...@googlegroups.com
#25304: Allow management commands to check if database migrations are applied
-------------------------------------+-------------------------------------
Reporter: mlorant | Owner:
| MounirMesselmeni
Type: New feature | Status: closed
Component: Core (Management | Version: 1.8
commands) |
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"1ac7fdcd136bbb7ae5476cb54f670be20178c4e2" 1ac7fdcd]:
{{{
#!CommitTicketReference repository=""
revision="1ac7fdcd136bbb7ae5476cb54f670be20178c4e2"
Refs #25304 -- Added assertion for Command.requires_migrations_checks
default.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/25304#comment:15>

Reply all
Reply to author
Forward
0 new messages