Tuesday, June 29, 2010

Creating a many-to-many link for a UserProfile?

Django 1.2.1, with Python 2.6

I am trying to establish a many-to-many for a userprofile (working
with a legacy database so some of the field names are a little
strange...).

The models I have are:

class UserGrouping(models.Model):
#many-to-many
id = models.AutoField(primary_key=True, db_column='id')
grouping = models.ForeignKey(Grouping, db_column='grouping_id')
user = models.ForeignKey(User, db_column='user_id')
class Meta:
db_table = 'usergrouping'
unique_together = (('grouping', 'user'), )

class UserProfile(models.Model):
user = models.ForeignKey(User, db_column='user_id', unique=True,
primary_key=True, blank=True, null=True, default=None)
userid = models.CharField(verbose_name=_('ID'), unique=True,
editable=False, max_length=6, blank=True, null=True)
records = models.IntegerField(db_column='records', default=20,
blank=True, null=True)
groupings = models.ManyToManyField(UserGrouping,
db_table="usergrouping")
class Meta:
db_table = 'user'

The table structures are as follows:

CREATE TABLE `usergrouping` (
`grouping_id` int(11) NOT NULL DEFAULT '0',
`user_id` int(11) NOT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`),
UNIQUE KEY `usergrouping_combo` (`grouping_id`,`user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8

CREATE TABLE `user` (
`user_id` int(11) DEFAULT NULL,
`userid` varchar(6) DEFAULT '',
`records` int(11) NOT NULL DEFAULT '10',
`LastChanged` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE
CURRENT_TIMESTAMP
) ENGINE=MyISAM DEFAULT CHARSET=utf8


The error I get is:

OperationalError at /admin/auth/user/895/
(1054, "Unknown column 'T2.userprofile_id' in 'where clause'")

I am not sure why Django is looking for a "userprofile_id", and
therefore how to go about creating the relationship? (If I remove the
'groupings' link, then the profile works as expected.)

Thanks
Derek

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment