Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions tests/_utils/setup.sql
Original file line number Diff line number Diff line change
Expand Up @@ -600,3 +600,13 @@ CREATE TABLE `delete_player_game` (
KEY (`player_id`),
KEY(`game_id`)
);

-- db_functions
CREATE TABLE `db_functions_article_author` (
`article_id` BIGINT NOT NULL,
`author_id` BIGINT NOT NULL,
SHARD KEY (`article_id`),
UNIQUE KEY (`article_id`, `author_id`),
KEY (`article_id`),
KEY (`author_id`)
);
37 changes: 31 additions & 6 deletions tests/db_functions/migrations/0002_create_test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ class Migration(migrations.Migration):
migrations.CreateModel(
name="Article",
fields=[
(
"authors",
models.ManyToManyField(
"db_functions.Author", related_name="articles"
),
),
("title", models.CharField(max_length=50)),
("summary", models.CharField(max_length=200, null=True, blank=True)),
("text", models.TextField()),
Expand All @@ -34,6 +28,37 @@ class Migration(migrations.Migration):
("views", models.PositiveIntegerField(default=0)),
],
),
migrations.CreateModel(
name="ArticleAuthor",
fields=[
(
"article",
models.ForeignKey(
on_delete=models.CASCADE, to="db_functions.article"
),
),
(
"author",
models.ForeignKey(
on_delete=models.CASCADE, to="db_functions.author"
),
),
],
options={
"managed": False,
"db_table": "db_functions_article_author",
"unique_together": {("article", "author")},
},
),
migrations.AddField(
model_name="article",
name="authors",
field=models.ManyToManyField(
related_name="articles",
through="db_functions.ArticleAuthor",
to="db_functions.author",
),
),
migrations.CreateModel(
name="Fan",
fields=[
Expand Down
11 changes: 10 additions & 1 deletion tests/db_functions/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Author(models.Model):


class Article(models.Model):
authors = models.ManyToManyField(Author, related_name="articles")
authors = models.ManyToManyField("Author", related_name="articles", through="ArticleAuthor")
title = models.CharField(max_length=50)
summary = models.CharField(max_length=200, null=True, blank=True)
text = models.TextField()
Expand All @@ -22,6 +22,15 @@ class Article(models.Model):
views = models.PositiveIntegerField(default=0)


class ArticleAuthor(models.Model):
article = models.ForeignKey(Article, on_delete=models.CASCADE)
author = models.ForeignKey(Author, on_delete=models.CASCADE)

class Meta:
unique_together = (('article', 'author'),)
db_table = "db_functions_article_author"


class Fan(models.Model):
name = models.CharField(max_length=50)
age = models.PositiveSmallIntegerField(default=30)
Expand Down
Loading