Opened 10 years ago

Closed 10 years ago

#22051 closed Bug (fixed)

Running `makemigrations` twice in a row fails

Reported by: Baptiste Mispelon Owned by: Andrew Godwin
Component: Migrations Version: dev
Severity: Release blocker Keywords:
Cc: numerodix@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

With the following (slightly contrived but still valid) models (taken from #22047):

from django.contrib.auth.models import User, Group
from django.db import models

class MyGroup(Group):
    users = models.ManyToManyField('bug22047.MyUser', through='Membership')


class MyUser(User):
    pass


class Membership(models.Model):
    user = models.ForeignKey(MyUser)
    group = models.ForeignKey(MyGroup)

Running manage.py makemigrations for the first time works, but running it immediately after raises this error:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "./django/core/management/__init__.py", line 427, in execute_from_command_line
    utility.execute()
  File "./django/core/management/__init__.py", line 419, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "./django/core/management/base.py", line 287, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "./django/core/management/base.py", line 336, in execute
    output = self.handle(*args, **options)
  File "./django/core/management/commands/makemigrations.py", line 99, in handle
    changes = autodetector.changes(graph=loader.graph, trim_to_apps=app_labels or None)
  File "./django/db/migrations/autodetector.py", line 33, in changes
    changes = self._detect_changes()
  File "./django/db/migrations/autodetector.py", line 50, in _detect_changes
    old_apps = self.from_state.render()
  File "./django/db/migrations/state.py", line 52, in render
    raise InvalidBasesError("Cannot resolve bases for %r" % new_unrendered_models)
django.db.migrations.state.InvalidBasesError: Cannot resolve bases for [<django.db.migrations.state.ModelState object at 0x7f2353215e90>, <django.db.migrations.state.ModelState object at 0x7f23531b7350>]

Change History (14)

comment:1 by Tim Graham, 10 years ago

Triage Stage: UnreviewedAccepted

comment:2 by Martin Matusiak, 10 years ago

Cc: numerodix@… added

comment:3 by Andrew Godwin, 10 years ago

Owner: set to Andrew Godwin
Status: newassigned

comment:4 by Andrew Godwin, 10 years ago

Resolution: fixed
Status: assignedclosed

Just tested this, it's no longer the case; previous fixes to state must have fixed it.

comment:5 by agbaid@…, 10 years ago

Just tried running makemigrations twice and it still failed with the same error messages

comment:6 by Andrew Godwin, 10 years ago

I'll need more than that if I'm going to debug it - the models you used, the full error output, the Django commit hash you're working from.

in reply to:  6 comment:7 by agbaid@…, 10 years ago

Replying to andrewgodwin:

I'll need more than that if I'm going to debug it - the models you used, the full error output, the Django commit hash you're working from.

Does this help?

from django.db import models
from django.contrib.auth.models import User
from datetime import date
import string 
import random

class FunStuffUser(User):
	address = models.CharField(max_length=256, blank=True)
	dob = models.DateField(default=date(1900, 1, 1))

class Music(models.Model):
	name = models.CharField(max_length=256)
	key = models.CharField(max_length=10, primary_key=True)

Here is the model and this is the error message I receive

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File ".\django-1.8.dev20140409113843-py3.3.egg\django\core\management\__init__.py", line 4
27, in execute_from_command_line
    utility.execute()
  File ".\django-1.8.dev20140409113843-py3.3.egg\django\core\management\__init__.py", line 4
19, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File ".\django-1.8.dev20140409113843-py3.3.egg\django\core\management\base.py", line 288,
in run_from_argv
    self.execute(*args, **options.__dict__)
  File "C:\Python33\lib\site-packages\django-1.8.dev20140409113843-py3.3.egg\django\core\management\base.py", line 337,
in execute
    output = self.handle(*args, **options)
  File ".\django-1.8.dev20140409113843-py3.3.egg\django\core\management\commands\makemigrati
ons.py", line 99, in handle
    changes = autodetector.changes(graph=loader.graph, trim_to_apps=app_labels or None)
  File ".\django-1.8.dev20140409113843-py3.3.egg\django\db\migrations\autodetector.py", line
 33, in changes
    changes = self._detect_changes()
  File ".\django-1.8.dev20140409113843-py3.3.egg\django\db\migrations\autodetector.py", line
 50, in _detect_changes
    old_apps = self.from_state.render()
  File ".\django-1.8.dev20140409113843-py3.3.egg\django\db\migrations\state.py", line 52, in
 render
    raise InvalidBasesError("Cannot resolve bases for %r" % new_unrendered_models)
django.db.migrations.state.InvalidBasesError: Cannot resolve bases for [<django.db.migrations.state.ModelState object at 0x0000000003A65940>]

comment:8 by Andrew Godwin, 10 years ago

Resolution: fixed
Status: closednew

comment:9 by anonymous, 10 years ago

I'm having this error now with the most recent 1.7 branch. (956bd644249337b9467c017aac4eec228dde8c5d) only with a different error:

(ph) ubuntu at placeholder in ~/newtest/bug_report (master●)
$ ./manage.py makemigrations
Traceback (most recent call last):
    File "./manage.py", line 10, in <module>
        execute_from_command_line(sys.argv)
    File "/home/ubuntu/.venvs/ph/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 427, in execute_from_command_line
        utility.execute()
    File "/home/ubuntu/.venvs/ph/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 419, in execute
        self.fetch_command(subcommand).run_from_argv(self.argv)
    File "/home/ubuntu/.venvs/ph/local/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
        self.execute(*args, **options.__dict__)
    File "/home/ubuntu/.venvs/ph/local/lib/python2.7/site-packages/django/core/management/base.py", line 337, in execute
        output = self.handle(*args, **options)
    File "/home/ubuntu/.venvs/ph/local/lib/python2.7/site-packages/django/core/management/commands/makemigrations.py", line 99, in handle
        changes = autodetector.changes(graph=loader.graph, trim_to_apps=app_labels or None)
    File "/home/ubuntu/.venvs/ph/local/lib/python2.7/site-packages/django/db/migrations/autodetector.py", line 33, in changes
        changes = self._detect_changes()
    File "/home/ubuntu/.venvs/ph/local/lib/python2.7/site-packages/django/db/migrations/autodetector.py", line 50, in _detect_changes
        old_apps = self.from_state.render()
    File "/home/ubuntu/.venvs/ph/local/lib/python2.7/site-packages/django/db/migrations/state.py", line 63, in render
        model=dangling_lookup[0]))
    ValueError: Lookup failed for model referenced by field report.Project.user: auth.User

(ph) ubuntu at placeholder in ~/newtest/bug_report (master●)
$ cat report/models.py
from django.db import models
from django.contrib.auth.models import User

class StringField(models.CharField):
    def __init__(self, *args, **kwargs):
        kwargs['max_length'] = 255
        return super(StringField, self).__init__(*args, **kwargs)

class Project(models.Model):

    user = models.ForeignKey(User)
    name = StringField()

comment:10 by david.fischer.ch@…, 10 years ago

I am facing the same bug.

That is a serious issue for my project.

comment:11 by drackett@…, 10 years ago

I'm running into the same thing as well. my initial makemigration is fine, but if I try and run it again (even with no changes) I get the same

django.db.migrations.state.InvalidBasesError: Cannot resolve bases for [<django.db.migrations.state.ModelState object at 0x0000000003A65940>]

as everyone else.

comment:12 by l.a.evil.genius@…, 10 years ago

If you are subclassing from User maybe this will help:
In your model's migration file find string: bases=('auth.user',), and replace it with bases=(User,),. And don't forget to import: from django.contrib.auth.models import User

comment:13 by drackett@…, 10 years ago

thanks l.a.evil.genius, the user fix did it for me.

comment:14 by Tim Graham, 10 years ago

Resolution: fixed
Status: newclosed

I think the bases error is a duplicate of #22447 as I cannot reproduce the error with the models in comment 7 and 9 on the latest master. Please open a new ticket if there are other issues.

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