-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Django 5.0](https://docs.djangoproject.com/en/5.0/releases/5.0/#database-computed-default-values) New Feature (db related): 1. Database-computed default values, TiDB has limited support for default value expressions, see [docs](https://docs.pingcap.com/tidb/dev/data-type-default-values#specify-expressions-as-default-values). 2. Database generated model field, TiDB also has some [limitations](https://docs.pingcap.com/tidb/stable/generated-columns). This PR also dropped support for TiDB 4.x as it has already reached its [EOL](https://www.pingcap.com/tidb-release-support-policy/).
- Loading branch information
Showing
13 changed files
with
368 additions
and
25 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ | |
|
||
from .patch import monkey_patch | ||
|
||
__version__ = "4.2.3" | ||
__version__ = "5.0.0" | ||
|
||
|
||
monkey_patch() |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# About | ||
|
||
This test is copied from the Django [field_defaults](https://github.com/django/django/tree/main/tests/field_defaults), as TiDB has some [limitations](https://docs.pingcap.com/tidb/dev/data-type-default-values#specify-expressions-as-default-values) on the default expression of the field, it does not support such many expressions as MySQL. |
Empty file.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
""" | ||
Callable defaults | ||
You can pass callable objects as the ``default`` parameter to a field. When | ||
the object is created without an explicit value passed in, Django will call | ||
the method to determine the default value. | ||
This example uses ``datetime.datetime.now`` as the default for the ``pub_date`` | ||
field. | ||
""" | ||
|
||
from datetime import datetime | ||
from decimal import Decimal | ||
|
||
from django.db import models | ||
from django.db.models.functions import Random, Now | ||
|
||
|
||
class Article(models.Model): | ||
headline = models.CharField(max_length=100, default="Default headline") | ||
pub_date = models.DateTimeField(default=datetime.now) | ||
|
||
def __str__(self): | ||
return self.headline | ||
|
||
|
||
class DBArticle(models.Model): | ||
""" | ||
Values or expressions can be passed as the db_default parameter to a field. | ||
When the object is created without an explicit value passed in, the | ||
database will insert the default value automatically. | ||
""" | ||
|
||
headline = models.CharField(max_length=100, db_default="Default headline") | ||
pub_date = models.DateTimeField(db_default=Now()) | ||
cost = models.DecimalField( | ||
max_digits=3, decimal_places=2, db_default=Decimal("3.33") | ||
) | ||
|
||
class Meta: | ||
required_db_features = {"supports_expression_defaults"} | ||
|
||
|
||
class DBDefaults(models.Model): | ||
both = models.IntegerField(default=1, db_default=2) | ||
null = models.FloatField(null=True, db_default=1.1) | ||
|
||
|
||
# This model has too many db_default expressions that TiDB does not support | ||
# class DBDefaultsFunction(models.Model): | ||
# number = models.FloatField(db_default=Pi()) | ||
# year = models.IntegerField(db_default=ExtractYear(Now())) | ||
# added = models.FloatField(db_default=Pi() + 4.5) | ||
# multiple_subfunctions = models.FloatField(db_default=Coalesce(4.5, Pi())) | ||
# case_when = models.IntegerField( | ||
# db_default=models.Case(models.When(GreaterThan(2, 1), then=3), default=4) | ||
# ) | ||
|
||
# class Meta: | ||
# required_db_features = {"supports_expression_defaults"} | ||
|
||
|
||
class TiDBDefaultsFunction(models.Model): | ||
number = models.DecimalField(max_digits=3, decimal_places=2, db_default=Random()) | ||
created_at = models.DateTimeField(db_default=Now()) | ||
|
||
|
||
class DBDefaultsPK(models.Model): | ||
language_code = models.CharField(primary_key=True, max_length=2, db_default="en") | ||
|
||
|
||
class DBDefaultsFK(models.Model): | ||
language_code = models.ForeignKey( | ||
DBDefaultsPK, db_default="fr", on_delete=models.CASCADE | ||
) |
Oops, something went wrong.