1

I added classes to Django app model to create mysql tables. I have 'downloads' table, and 'downloads' column in 'songs' table.

When i want to sync db, Django returns me error:

CommandError: One or more models did not validate:
mp3mid.downloads: Reverse query name for field 'song' clashes with field 'songs.downloads'. Add a related_name argument to the definition for 'song'.

Why it's impossible to give same name to table and column?

this is my models.py:

from django_evolution.mutations import *
from django.db import models

class singers(models.Model):
    name = models.CharField(max_length = 255)
    category = models.ForeignKey(categories)

class songs(models.Model):
    name = models.CharField(max_length = 255)
    singer = models.ForeignKey(singers)
    downloads = models.IntegerField(max_length = 11)
    exclusive = models.BooleanField(default = 0)
    hit = models.BooleanField(default = 0)
    date = models.DateTimeField(auto_now_add = True)
    link = models.CharField(max_length = 255)

class downloads(models.Model):
    song = models.ForeignKey(songs)
    date = models.DateTimeField(auto_now_add = True)
3
  • 1
    could you post your model classes Commented May 11, 2013 at 16:39
  • 1
    what about this stackoverflow.com/questions/2608017/… Commented May 11, 2013 at 16:52
  • @dm03514 added models.py file. Commented May 12, 2013 at 14:39

1 Answer 1

1

Django allows you to make queries that span relationships.

In your case, the foreign key from downloads to songs means you could usually make queries that follow the relationship backwards from song to downloads:

from datetime import datetime
# fetch all songs with a download with a date on or after 2013-05-01.
songs = song.objects.filter(downloads__date__gte=datetime(2013,5,1))

However, you can't do that in this case, because downloads clashes with your songs.downloads field.

You have a couple of options. First, you can set related_name for your foreign key, as suggested by your error message.

class downloads(models.Model):
    song = models.ForeignKey(songs, related_name="related_downloads")

Or, you can rename your song.downloads field.

class songs(models.Model):
    ...
    num_downloads = models.IntegerField(max_length = 11)

As an aside, I recommend you rename your models to Singer, Song and Download (capitalized, singular instead of plural), to follow the Django convention.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.