Code

Opened 7 months ago

Closed 7 months ago

#21215 closed Bug (invalid)

Migrations for inherited models

Reported by: anant90@… Owned by:
Component: Migrations Version: master
Severity: Normal Keywords: migrations inherited model
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I have a model BankAccount which inherits from another model PaymentType as shown below:

class PaymentType(BaseModel):
	user = models.ForeignKey(StupaUser, related_name = "%(app_label)s_%(class)s_related")

class BankAccount(PaymentType):
	account_number = models.CharField(max_length=20)
	bank_name = models.CharField(max_length=40)
	nickname = models.CharField(max_length=30)
	uri = models.CharField(max_length=200, null=True) 
	is_valid = models.BooleanField(default=True)

When I try running python manage.py makemigrations <app_name>, I get "ValueError: No field called user on model BankAccount"

Here's the stack trace:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/anant/Github/django/django/core/management/__init__.py", line 397, in execute_from_command_line
    utility.execute()
  File "/Users/anant/Github/django/django/core/management/__init__.py", line 390, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/anant/Github/django/django/core/management/base.py", line 242, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/Users/anant/Github/django/django/core/management/base.py", line 289, in execute
    output = self.handle(*args, **options)
  File "/Users/anant/Github/django/django/core/management/commands/makemigrations.py", line 52, in handle
    changes = autodetector.changes(graph=loader.graph, trim_to_apps=app_labels or None)
  File "/Users/anant/Github/django/django/db/migrations/autodetector.py", line 34, in changes
    changes = self._detect_changes()
  File "/Users/anant/Github/django/django/db/migrations/autodetector.py", line 140, in _detect_changes
    field = model_state.get_field_by_name(field_name),
  File "/Users/anant/Github/django/django/db/migrations/state.py", line 177, in get_field_by_name
    raise ValueError("No field called %s on model %s" % (name, self.name))
ValueError: No field called user on model BankAccount

Is this expected? Why can't I use migrations for inherited models?

Attachments (0)

Change History (4)

comment:1 Changed 7 months ago by timo

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

I'm unable to reproduce this with your given models (changing BaseModel to models.Model and the user FK django.contrib.auth.models.User to a different model).

Are you running with the latest master? (migrations is under active development)

If so, could you provide a minimal models file to reproduce?

comment:2 Changed 7 months ago by anant90@…

Thanks for the quick reply. I made a new testapp with the following models.py file:

`
from django.db import models
from django.contrib.auth.models import User, UserManager

# Create your models here.
class PaymentType(models.Model):

user = models.ForeignKey(User, related_name = "%(app_label)s_%(class)s_related")

class BankAccount(PaymentType):

account_number = models.CharField(max_length=20)
bank_name = models.CharField(max_length=40)
nickname = models.CharField(max_length=30)
uri = models.CharField(max_length=200, null=True) #balanced uri
is_valid = models.BooleanField(default=True)

`

Running python manage.py makemigrations testapp gives the following output:

Traceback (most recent call last):

File "manage.py", line 10, in <module>

execute_from_command_line(sys.argv)

File "/Users/anant/Github/django/django/core/management/init.py", line 397, in execute_from_command_line

utility.execute()

File "/Users/anant/Github/django/django/core/management/init.py", line 390, in execute

self.fetch_command(subcommand).run_from_argv(self.argv)

File "/Users/anant/Github/django/django/core/management/base.py", line 242, in run_from_argv

self.execute(*args, options.dict)

File "/Users/anant/Github/django/django/core/management/base.py", line 289, in execute

output = self.handle(*args, options)

File "/Users/anant/Github/django/django/core/management/commands/makemigrations.py", line 52, in handle

changes = autodetector.changes(graph=loader.graph, trim_to_apps=app_labels or None)

File "/Users/anant/Github/django/django/db/migrations/autodetector.py", line 34, in changes

changes = self._detect_changes()

File "/Users/anant/Github/django/django/db/migrations/autodetector.py", line 140, in _detect_changes

field = model_state.get_field_by_name(field_name),

File "/Users/anant/Github/django/django/db/migrations/state.py", line 177, in get_field_by_name

raise ValueError("No field called %s on model %s" % (name, self.name))

ValueError: No field called user on model BankAccount

I pulled in the latest django version on master and used pip install -e django/ for installation. Also, I use virtualenv.

Thanks for your time. Let me know if I'm doing something wrong here.

comment:3 Changed 7 months ago by timo

Using those test models, I'm able to successfully generate a migrations file so I'm not sure what's going on.

comment:4 Changed 7 months ago by anant90@…

  • Resolution set to invalid
  • Status changed from new to closed

Thanks for your help. I was able to get it working with a clean django installation. Closing this issue as invalid.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.