Colin Wood Colin Wood - 7 months ago 113
MySQL Question

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!

Answer Source

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()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download