Last active
September 6, 2019 01:15
-
-
Save ryu22e/2133a709c36d17b55301165f49698357 to your computer and use it in GitHub Desktop.
SQLアンチパターン for Django 2章 ナイーブツリー(素朴な木)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""悪い例""" | |
from django.conf import settings | |
from django.db import models | |
class Bug(models.Model): | |
pass | |
class Comment(models.Model): | |
parent = models.ForeignKey(to='chapter2.Comment', on_delete=models.CASCADE) | |
bug = models.ForeignKey(to=Bug, on_delete=models.CASCADE) | |
author = models.ForeignKey(to=settings.AUTH_USER_MODEL, on_delete=models.PROTECT) | |
comment_date = models.DateTimeField(auto_now_add=True) | |
comment_text = models.TextField() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""良い例1""" | |
from django.conf import settings | |
from django.db import models | |
class Bug(models.Model): | |
pass | |
class Comment(models.Model): | |
bug = models.ForeignKey(to=Bug, on_delete=models.CASCADE) | |
path = models.CharField(max_length=1000) | |
author = models.ForeignKey(to=settings.AUTH_USER_MODEL, on_delete=models.PROTECT) | |
comment_date = models.DateTimeField(auto_now_add=True) | |
comment_text = models.TextField() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""良い例2""" | |
from django.conf import settings | |
from django.db import models | |
class Bug(models.Model): | |
pass | |
class Comment(models.Model): | |
bug = models.ForeignKey(to=Bug, on_delete=models.CASCADE) | |
nsleft = models.PositiveIntegerField() | |
nsright = models.PositiveIntegerField() | |
author = models.ForeignKey(to=settings.AUTH_USER_MODEL, on_delete=models.PROTECT) | |
comment_date = models.DateTimeField(auto_now_add=True) | |
comment_text = models.TextField() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""良い例3""" | |
from django.conf import settings | |
from django.db import models | |
class Bug(models.Model): | |
pass | |
class Comment(models.Model): | |
bug = models.ForeignKey(to=Bug, on_delete=models.CASCADE) | |
author = models.ForeignKey(to=settings.AUTH_USER_MODEL, on_delete=models.PROTECT) | |
comment_date = models.DateTimeField(auto_now_add=True) | |
comment_text = models.TextField() | |
class TreePath(models.Model): | |
ancestor = models.ForeignKey( | |
to=Comment, | |
on_delete=models.CASCADE, | |
related_name='ancestor', | |
) | |
descendant = models.ForeignKey( | |
to=Comment, | |
on_delete=models.CASCADE, | |
related_name='descendant', | |
) | |
class Meta: | |
unique_together = [ | |
['ancestor', 'descendant'], | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment