mirror of
https://github.com/django/django.git
synced 2025-10-27 07:36:08 +00:00
Fixed #373 -- Added CompositePrimaryKey.
Thanks Lily Foote and Simon Charette for reviews and mentoring this Google Summer of Code 2024 project. Co-authored-by: Simon Charette <charette.s@gmail.com> Co-authored-by: Lily Foote <code@lilyf.org>
This commit is contained in:
committed by
Sarah Boyce
parent
86661f2449
commit
978aae4334
50
tests/composite_pk/models/tenant.py
Normal file
50
tests/composite_pk/models/tenant.py
Normal file
@@ -0,0 +1,50 @@
|
||||
from django.db import models
|
||||
|
||||
|
||||
class Tenant(models.Model):
|
||||
name = models.CharField(max_length=10, default="", blank=True)
|
||||
|
||||
|
||||
class Token(models.Model):
|
||||
pk = models.CompositePrimaryKey("tenant_id", "id")
|
||||
tenant = models.ForeignKey(Tenant, on_delete=models.CASCADE, related_name="tokens")
|
||||
id = models.SmallIntegerField()
|
||||
secret = models.CharField(max_length=10, default="", blank=True)
|
||||
|
||||
|
||||
class BaseModel(models.Model):
|
||||
pk = models.CompositePrimaryKey("tenant_id", "id")
|
||||
tenant = models.ForeignKey(Tenant, on_delete=models.CASCADE)
|
||||
id = models.SmallIntegerField(unique=True)
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
|
||||
class User(BaseModel):
|
||||
email = models.EmailField(unique=True)
|
||||
|
||||
|
||||
class Comment(models.Model):
|
||||
pk = models.CompositePrimaryKey("tenant", "id")
|
||||
tenant = models.ForeignKey(
|
||||
Tenant,
|
||||
on_delete=models.CASCADE,
|
||||
related_name="comments",
|
||||
)
|
||||
id = models.SmallIntegerField(unique=True, db_column="comment_id")
|
||||
user_id = models.SmallIntegerField()
|
||||
user = models.ForeignObject(
|
||||
User,
|
||||
on_delete=models.CASCADE,
|
||||
from_fields=("tenant_id", "user_id"),
|
||||
to_fields=("tenant_id", "id"),
|
||||
related_name="comments",
|
||||
)
|
||||
text = models.TextField(default="", blank=True)
|
||||
|
||||
|
||||
class Post(models.Model):
|
||||
pk = models.CompositePrimaryKey("tenant_id", "id")
|
||||
tenant = models.ForeignKey(Tenant, on_delete=models.CASCADE)
|
||||
id = models.UUIDField()
|
||||
Reference in New Issue
Block a user