Colin Wood
Colin Wood

Reputation: 467

Django and DB error "Column 'id' specified twice"

I am new to django and I am having some issues when I try to save a mapping.

from django.db import models

# Create your models here.
class Users(models.Model):
        user = models.CharField(max_length=45)
        password = models.CharField(max_length=125)
        env = models.CharField(max_length=10)
        class Meta:
                db_table = 'esp-users'
        def __unicode__(self):
                return unicode(self.user)
class Groups(models.Model):
        group_name = models.CharField(max_length=45)
        description = models.CharField(max_length=255)
        env = models.CharField(max_length=45)
        class Meta:
                db_table = 'esp-groups'
        def __unicode__(self):
                return unicode(self.group_name)
class Roles(models.Model):
        role_name = models.CharField(max_length=45)
        env = models.CharField(max_length=10)
        class Meta:
                db_table = 'esp-roles'
        def __unicode__(self):
                return unicode(self.role_name)
class Group_Map(models.Model):
        group_id = models.ForeignKey(Groups, db_column='id')
        user_id = models.ForeignKey(Users, db_column='id')
        class Meta:
                db_table='esp-group-map'
        def __unicode__(self):
                return unicode(self.group_id)
class Role_Map(models.Model):
        role_id = models.ForeignKey(Roles, db_column='id')
        group_id = models.ForeignKey(Groups, db_column='id')
        class Meta:
                db_table='esp-role-map'
        def __unicode__(self):
                return unicode(self.role_id)

Thanks for the help.

Django V 1.3, Python 2.4, Mysql 5.0.77

Request Method: POST Request URL: http://somehost:8100/admin/users_admin/group_map/add/ Django Version: 1.3 Exception Type: DatabaseError Exception Value:

(1110, "Column 'id' specified twice")

Lesson Learned: If its a new database isntead of making the tables yourself just run python manage.py syncdb and it will make the db for you and all is good. Thanks for the help!

Upvotes: 0

Views: 3040

Answers (4)

Eric Sellin
Eric Sellin

Reputation: 668

group = models.ForeignKey(Groups, db_column='group_id')
user = models.ForeignKey(Users, db_column='user_id')

Upvotes: 1

vartec
vartec

Reputation: 134701

db_column specifies the column name to use in the model you're defining, not the referred one. There is no need to specify column names for FKs, it's done automatically.

Upvotes: 0

Spike
Spike

Reputation: 5130

You are specifying db_column='id' for both group_id and for user_id. You can't have two columns with the same name. You can remove this parameter and let Django take care of the naming for you.

Upvotes: 0

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181460

You can't do this:

group_id = models.ForeignKey(Groups, db_column='id')
user_id = models.ForeignKey(Users, db_column='id')

You just need to do:

group_id = models.ForeignKey(Groups)
user_id = models.ForeignKey(Users)

Django will infer what column to use based on Users and Groups primary keys.

While you are at it, I would recommend you to change Groups into Group, Users into User and so on, so forth. Your code will be more clear since you will do stuff like:

user = User()

Instead of:

user = Users()

Upvotes: 7

Related Questions