Skip to content

Introduce SqlDefault for using expression as db_default#2106

Merged
abondar merged 1 commit intodevelopfrom
featuer/sql-default
Feb 12, 2026
Merged

Introduce SqlDefault for using expression as db_default#2106
abondar merged 1 commit intodevelopfrom
featuer/sql-default

Conversation

@abondar
Copy link
Member

@abondar abondar commented Feb 12, 2026

Resolves #2104

Introduce SqlDefault to pass expressions as db default
Change schema generator behaviour to match migrations (no default in db on default in Field)

AI Summary:
This pull request introduces support for raw SQL default expressions in database schemas via new SqlDefault and Now expressions, and clarifies the distinction between Python-only defaults and database-level defaults. It also updates documentation and tests to reflect these changes, and adjusts schema generation so that Python-only defaults (such as default=..., auto_now, and auto_now_add) do not emit DEFAULT clauses in generated schemas.

New features and API additions:

  • Added SqlDefault and Now expressions for use with the db_default parameter, allowing raw SQL expressions (e.g., CURRENT_TIMESTAMP) as database defaults. Now() is a shorthand for SqlDefault("CURRENT_TIMESTAMP"). [1] [2] [3] [4] [5] [6]

Behavior changes and schema generation:

  • Updated schema generation so that Field(default=...), auto_now, and auto_now_add no longer emit a DEFAULT clause in generate_schemas(). Only db_default is used for database-level defaults, aligning schema generation with migration behavior. [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12]

Documentation improvements:

  • Updated documentation to clarify the difference between Python-only defaults and database-level defaults, and provided usage examples for db_default, SqlDefault, and Now. [1] [2] [3]

Testing:

  • Added comprehensive tests for SqlDefault and Now, including construction, equality, hashing, schema generation, migration rendering, and serialization.

Examples and migrations:

  • Updated example models and migrations to demonstrate the use of SqlDefault and Now in real project scenarios. [1] [2] [3]

@codspeed-hq
Copy link

codspeed-hq bot commented Feb 12, 2026

Merging this PR will not alter performance

✅ 24 untouched benchmarks


Comparing featuer/sql-default (e61f6ea) with develop (e119288)

Open in CodSpeed

@coveralls
Copy link

Pull Request Test Coverage Report for Build 21944692887

Details

  • 47 of 61 (77.05%) changed or added relevant lines in 12 files are covered.
  • 13 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-0.1%) to 82.523%

Changes Missing Coverage Covered Lines Changed/Added Lines %
tortoise/backends/oracle/schema_generator.py 0 1 0.0%
tortoise/migrations/expressions.py 0 1 0.0%
tortoise/backends/base/schema_generator.py 4 6 66.67%
tortoise/migrations/schema_editor/sqlite.py 0 10 0.0%
Files with Coverage Reduction New Missed Lines %
tortoise/converters.py 13 44.55%
Totals Coverage Status
Change from base Build 21925286894: -0.1%
Covered Lines: 10578
Relevant Lines: 12351

💛 - Coveralls

@abondar abondar merged commit 12a391f into develop Feb 12, 2026
25 checks passed
@abondar abondar deleted the featuer/sql-default branch February 12, 2026 11:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Different table schema from generate_schema() vs. migration

2 participants

Comments