jeff m
jeff m

Reputation: 275

Local field clashes with field of similar name

I'm trying to add a new database model that will let me "group" expenses, but am running across this issue when running python manage.py makemigrations

My virtual environment looks like this:

Django==1.7.3
argparse==1.2.1
django-braces==1.4.0
django-chartit==0.1
django-crispy-forms==1.4.0
django-debug-toolbar==1.2.2
psycopg2==2.6
six==1.9.0
sqlparse==0.1.14
wsgiref==0.1.2

Here is the traceback:

    Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/jeff/.virtualenvs/ggc/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/home/jeff/.virtualenvs/ggc/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/jeff/.virtualenvs/ggc/local/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/jeff/.virtualenvs/ggc/local/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/home/jeff/.virtualenvs/ggc/local/lib/python2.7/site-packages/django/core/management/commands/makemigrations.py", line 111, in handle
    convert_apps=app_labels or None,
  File "/home/jeff/.virtualenvs/ggc/local/lib/python2.7/site-packages/django/db/migrations/autodetector.py", line 42, in changes
    changes = self._detect_changes(convert_apps, graph)
  File "/home/jeff/.virtualenvs/ggc/local/lib/python2.7/site-packages/django/db/migrations/autodetector.py", line 109, in _detect_changes
    self.old_apps = self.from_state.render(ignore_swappable=True)
  File "/home/jeff/.virtualenvs/ggc/local/lib/python2.7/site-packages/django/db/migrations/state.py", line 67, in render
    model.render(self.apps)
  File "/home/jeff/.virtualenvs/ggc/local/lib/python2.7/site-packages/django/db/migrations/state.py", line 316, in render
    body,
  File "/home/jeff/.virtualenvs/ggc/local/lib/python2.7/site-packages/django/db/models/base.py", line 229, in __new__
    'base class %r' % (field.name, name, base.__name__)
django.core.exceptions.FieldError: Local field u'id' in class 'ExpenseGroup' clashes with field of similar name from base class 'Asset

'

This is the model code - I've tried cutting the ExpenseGroup model down to only the groupName as a field but I get the same error. What am I missing?

    class Asset(TimeStampedModel):
        assetName    = models.CharField(max_length=255)
        assetAddress = models.CharField(max_length=255)
        slug         = models.SlugField(max_length=255, blank=True)

        class Meta:
            unique_together = ('assetName', 'assetAddress')

        def __str__(self):
            return self.assetName

        def save(self, *args, **kwargs):
            self.slug = slugify(self.assetName)
            super(Asset, self).save(*args, **kwargs)


class ExpenseGroup(TimeStampedModel):
    groupName          = models.CharField(max_length=255, blank=False)
    expenseName        = models.ForeignKey(Expense, related_name='expenseGroup')

    class Meta:
        unique_together = ('expenseName', 'groupName')

    def __str__(self):
        return self.groupName

    def save(self, *args, **kwargs):
        return super(ExpenseGroup, self).save(*args, **kwargs)



class Expense(TimeStampedModel):
    assetName          = models.ForeignKey(Asset, related_name='assetExpense')
    category           = models.ForeignKey(ExpenseCategory, related_name='expenseCategory')
    expensePeriod      = models.DateTimeField(blank=False)
    expenseName        = models.CharField(max_length=255, blank=False)
    expenseAmount      = models.DecimalField(max_digits=20, decimal_places=2, blank=True)

    class Meta:
        unique_together = ('expenseName', 'expensePeriod')

    def __str__(self):
        return self.expenseName

    def save(self, *args, **kwargs):
        super(Expense, self).save(*args, **kwargs)

Upvotes: 2

Views: 6377

Answers (3)

Esteban
Esteban

Reputation: 2513

There's more to this than what you posted. Can you post your entire models file? or a link to a gist? I'm surprised you are not getting an error about the FK reference to 'Expense' from the 'ExpenseGroup' model, as it's defined later in the file.

Do you have any existing migrations? I would suggest deleting any of your existing migrations and all your pyc files and trying again.

Upvotes: 0

Chrismit
Chrismit

Reputation: 1518

It's a bug with Django 1.7.3, upgrade to the latest and you'll be fine (1.7.5 at the time)

Upvotes: 1

Du D.
Du D.

Reputation: 5300

Can you post the TimeStampedModel definition? I suspect that you didn't declare the base model as Abstract. That why the "id" fields are conflicted with each others.

class TimeStampedModel(models.Model):

    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)

    class Meta:
        abstract = True

Upvotes: 2

Related Questions