Skip to content

Commit 33bb44f

Browse files
authored
Add db_default for fields (#2101)
1 parent 1420fb0 commit 33bb44f

File tree

13 files changed

+782
-6
lines changed

13 files changed

+782
-6
lines changed

CHANGELOG.rst

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,22 @@ Changelog
1010
1.0
1111
===
1212

13-
1.0.1 (unreleased)
14-
------------------
13+
1.1.0
14+
-----
15+
16+
Added
17+
^^^^^
18+
- **``db_default`` parameter for fields** — set database-level ``DEFAULT`` clauses that propagate to schema generation and migrations. Unlike ``default`` (Python-only), ``db_default`` is persisted in the DB schema and applied even for rows inserted outside the ORM. (#2101)
19+
- ``Model.construct()`` classmethod for building model instances without field validation — useful in test factories and fixtures. (#2099)
20+
- ``truncate_all_models()`` now respects foreign key constraints using topological ordering (SQLite/MySQL) or ``TRUNCATE ... CASCADE`` (PostgreSQL). (#2100)
21+
- Auto-recreate database connection when event loop changes. Enables easier testing without session level fixtures (#2098)
22+
1523
Fixed
1624
^^^^^
17-
- Type checking of None assignment to nullable fields (#2089)
18-
- Fix set global fallback default in Sanic register_tortoise (#2090)
25+
- Type checking of ``None`` assignment to nullable fields. (#2089)
26+
- Fix ``set global fallback default`` in Sanic ``register_tortoise``. (#2090)
27+
- Escape ``[`` ``]`` for db url parsing. (#2081) (#2092)
28+
- Fix ``UnicodeEncodeError`` by using UTF-8 encoding for migration files. (#2096, #2097)
1929

2030

2131
1.0.0
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from tortoise import fields, migrations
2+
from tortoise.migrations import operations as ops
3+
4+
5+
class Migration(migrations.Migration):
6+
dependencies = [("erp", "0017_create_bad_sorted_fk")]
7+
8+
initial = False
9+
10+
operations = [
11+
ops.AlterField(
12+
model_name="Product",
13+
name="is_active",
14+
field=fields.BooleanField(default=True, db_default=True),
15+
),
16+
ops.AddField(
17+
model_name="Product",
18+
name="stock_quantity",
19+
field=fields.IntField(db_default=0),
20+
),
21+
]
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from tortoise import fields, migrations
2+
from tortoise.migrations import operations as ops
3+
4+
5+
class Migration(migrations.Migration):
6+
dependencies = [("erp", "0018_add_db_defaults")]
7+
8+
initial = False
9+
10+
operations = [
11+
ops.AlterField(
12+
model_name="Product",
13+
name="stock_quantity",
14+
field=fields.IntField(db_default=10),
15+
),
16+
]
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from tortoise import fields, migrations
2+
from tortoise.migrations import operations as ops
3+
4+
5+
class Migration(migrations.Migration):
6+
dependencies = [("erp", "0019_change_db_default")]
7+
8+
initial = False
9+
10+
operations = [
11+
ops.AlterField(
12+
model_name="Product",
13+
name="is_active",
14+
field=fields.BooleanField(default=True),
15+
),
16+
]

examples/comprehensive_migrations_project/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ class Product(models.Model):
116116
rating = fields.FloatField(null=True, description="Average customer rating 0.0-5.0")
117117
processing_time = fields.TimeDeltaField(null=True, description="Average time to fulfill")
118118
is_active = fields.BooleanField(default=True)
119+
stock_quantity = fields.IntField(db_default=10)
119120
created_at = fields.DatetimeField(auto_now_add=True)
120121

121122
def __str__(self) -> str:

0 commit comments

Comments
 (0)