0

A duplicate model field is giving me trouble (no such table appname_modelname when I run my webpage). Whenever I do ./manage.py migrate appname, it gives me "duplicate field". I checked my models.py, there is only one of them there. How do I delete that duplicate field? It seems no matter what I do, it stays. I've tried:

  • Deleting the database

  • Deleting migrations folder in app folder

  • Doing ./manage.py sqlclear south and then dropping the south_migrationhistory table in the dbshell

  • ./manage.py schemamigration appname --initial, ./manage.py migrate appname --fake

I've run out of ideas.

class Document(models.Model):
    filename = models.CharField(max_length=255, blank=True, null=True, default=None)
    identity = models.CharField(max_length=255, default=None, null=True)
    user = models.ForeignKey(User, null=False)
    user_id = models.IntegerField(User, null=True)
    docfile = models.FileField(upload_to=_upload_path, storage=fs) # upload_to is a path inside the storage path

    def get_upload_path(self,filename):
        return str(self.user.id) + '/' + str(date.today()) + '/' + filename
Maxime Lorant
  • 34,607
  • 19
  • 87
  • 97
KSHMR
  • 741
  • 1
  • 9
  • 24
  • Please post the model in which duplicate field error is coming – Pawan Jul 14 '14 at 11:26
  • Ok I have edited my OP. Duplicate is user_id – KSHMR Jul 14 '14 at 11:28
  • Why user_id seems to be necessary ? user and user_id are both same fields. – Pawan Jul 14 '14 at 11:29
  • I would get an error saying this field was missing if I didn't a long time ago. I found this really strange but I gave up on debugging it because I couldn't find the error. – KSHMR Jul 14 '14 at 11:30
  • Hm - this is strange. I deleted the database again and did the things I said in the OP again and now it works... Thanks for spending time on this though! – KSHMR Jul 14 '14 at 11:33
  • Very much strange , I think I can help you , so in user field , you want user id who created document , right? – Pawan Jul 14 '14 at 11:34
  • 2
    just keep one field user = models.ForeignKey(User).. – Pawan Jul 14 '14 at 11:34
  • IntegerField dont required to add User model as reference (as this not making any sence). `models.IntegerField(User, null=True)` instead just use `models.IntegerField(null=True)` – MaNKuR Jul 14 '14 at 13:37

2 Answers2

5

You can't do this, for your user foreign key, Django ORM will create a database field named user_id (your foreign key field name plus _id) to use it as a FK in the database.

You don't have to create this field yourself (the ORM will take care), even if you need it, change the name of the attribute user or user_id.

From the documentation:

Behind the scenes, Django appends "_id" to the field name to create its database column name. In the above example, the database table for the Car model will have a manufacturer_id column. (You can change this explicitly by specifying db_column) However, your code should never have to deal with the database column name, unless you write custom SQL. You’ll always deal with the field names of your model object.

Maxime Lorant
  • 34,607
  • 19
  • 87
  • 97
Mounir
  • 11,306
  • 2
  • 27
  • 34
-1

Not sure but problem causing here in these two line

user = models.ForeignKey(User, null=False)
user_id = models.IntegerField(User, null=True)

Better to use "related name" attribute to avoid the duplicate error as in database "user" will be added as user_id.

user = models.ForeignKey(User, related_name="id_user") # Change the related field as your convenience
user_id = models.IntegerField(null=True, related_name="user_id")

Check if this resolve your issues

MaNKuR
  • 2,578
  • 1
  • 19
  • 31