Wait... I just looked at your sql and it looks like you would need to use multiple-column primary keys which are unsupported by django. http://code.djangoproject.com/ticket/373
On Oct 15, 4:15 am, Alexander <[email protected]> wrote: > I have a database, which among others has the following tables: > > CREATE TABLE users ( > userId BIGINT PRIMARY KEY > ); > > CREATE TABLE movies ( > movieId BIGINT PRIMARY KEY, > title varchar(255) NOT NULL > ); > > CREATE TABLE ratings ( > userId BIGINT NOT NULL REFERENCES users(userId), > movieId BIGINT NOT NULL REFERENCES movies(movieId), > rating SMALLINT NOT NULL, > timestamp varchar(150) NOT NULL, > CONSTRAINT unique_userid_movieid UNIQUE(userId, movieId) > ); > > As you can see the 'rating' table has no separate primary key field. > > Here are the models created by Django with some my corrections: > > class Users(models.Model): > userid = models.BigIntegerField(primary_key=True) > > def __unicode__(self): > return u"User %d" % self.userid > > class Meta: > db_table = u'users' > > class Movies(models.Model): > movieid = models.BigIntegerField(primary_key=True) > title = models.CharField(max_length=255) > users = models.ManyToManyField(Users, through='Ratings', > related_name="rated_movies") > > def __unicode__(self): > return self.title > > class Meta: > db_table = u'movies' > > class Ratings(models.Model): > user = models.ForeignKey(Users, db_column='userid') > movie = models.ForeignKey(Movies, db_column='movieid') > rating = models.SmallIntegerField() > timestamp = models.BigIntegerField() > > def __unicode__(self): > return u"%d" % self.rating > > class Meta: > db_table = u'ratings' > unique_together = (("user", "movie"),) > > So having these models i can do this: > > In [1]: from django_orm.movielens.models import * > In [2]: u = Users.objects.get(pk=1) > In [3]: u.rated_movies.all() > Out[3]: [<Movies: Boomerang (1992)>,..] > > or this: > > In [4]: m = Movies.objects.get(pk=1) > In [5]: m.users.all().count() > Out[5]: 2264 > > But i can't get the "ratings" objects because trying this: > > Ratings.objects.filter(user=1) > > or this: > > u.ratings_set.all() > > causes the following error > > DatabaseError: column ratings.id does not exist > LINE 1: SELECT "ratings"."id", "ratings"."userid", > "ratings"."moviei... > > Is there a way to tell Django, that 'Ratings' models does not have a > separate primary key? Or my only option is to add the column to the > 'ratings' table? -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

