具有复合键的模型之间的关系

发布于 2024-11-19 05:05:18 字数 1176 浏览 3 评论 0原文

我有两个带有复合键的模型:

class ContestUser(models.Model):
    user_id = models.IntegerField(primary_key = True)
    contest_id = models.IntegerField(primary_key = True)
    username = models.CharField(max_length = 1536, blank = True)
    .
    .
    .



class ContestRegistration(models.Model):
    user_id = models.IntegerField(primary_key = True)
    contest_id = models.IntegerField(primary_key = True)
    status = models.IntegerField(choices = EJUDGE_CONTEST_STATUSES)
    .
    .
    .

第一个问题是如何将它们关联起来,并像连接中那样进行查询。

Select * from ContestRegistration r join ContestUser u on r.user_id = u.user_id and r.contest_id = u.contest_id where r.contest_id = 3;

其次是如何保存这样的对象?

cuser = ContestUser.objects.get(user_id = 1, contest_id = 1)
cuser.username = 'username'
cuser.save()

这会导致 IntegrityError: (1062, "Duplicate entry '1-1' for key 'PRIMARY'")

执行的 SQL 是:

SELECT * FROM `users` WHERE (`users`.`contest_id` = 1  AND `users`.`user_id` = 1 );
SELECT (1) AS `a` FROM `users` WHERE `users`.`user_id` = 1  LIMIT 1;
UPDATE `users` SET ... WHERE `users`.`user_id` = 1 ;

I have two models with composite keys:

class ContestUser(models.Model):
    user_id = models.IntegerField(primary_key = True)
    contest_id = models.IntegerField(primary_key = True)
    username = models.CharField(max_length = 1536, blank = True)
    .
    .
    .



class ContestRegistration(models.Model):
    user_id = models.IntegerField(primary_key = True)
    contest_id = models.IntegerField(primary_key = True)
    status = models.IntegerField(choices = EJUDGE_CONTEST_STATUSES)
    .
    .
    .

First question is How can I relate them, and query like in join.

Select * from ContestRegistration r join ContestUser u on r.user_id = u.user_id and r.contest_id = u.contest_id where r.contest_id = 3;

Second is How to save an object like this?

cuser = ContestUser.objects.get(user_id = 1, contest_id = 1)
cuser.username = 'username'
cuser.save()

This results in IntegrityError: (1062, "Duplicate entry '1-1' for key 'PRIMARY'")

Executed SQL is:

SELECT * FROM `users` WHERE (`users`.`contest_id` = 1  AND `users`.`user_id` = 1 );
SELECT (1) AS `a` FROM `users` WHERE `users`.`user_id` = 1  LIMIT 1;
UPDATE `users` SET ... WHERE `users`.`user_id` = 1 ;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妄断弥空 2024-11-26 05:05:18

Django 模型不支持多个主键: https://docs.djangoproject.com/en/1.3/faq/models/#do-django-models-support-multiple-column-primary-keys

但是,正如文档所述,您可以在外键字段上使用其他属性,例如 unique_together 来执行相同的操作。希望对您有所帮助。

Django models don't support multiple primary keys: https://docs.djangoproject.com/en/1.3/faq/models/#do-django-models-support-multiple-column-primary-keys

However, as the documentation describes, you can use other properties on ForeignKey fields, like unique_together to do the same thing. Hope that helps you out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文