Skip to content

Add db_collation argument for fields - #2266

Open
ChrisJr404 wants to merge 1 commit into
tortoise:developfrom
ChrisJr404:add-db-collation
Open

Add db_collation argument for fields#2266
ChrisJr404 wants to merge 1 commit into
tortoise:developfrom
ChrisJr404:add-db-collation

Conversation

@ChrisJr404

Copy link
Copy Markdown

Description

Adds a db_collation argument to fields so you can set a column collation without hand writing SQL. When it is set, the generated schema gets a COLLATE clause, and it defaults to None so nothing changes for existing models.

The example from the issue works now:

name = fields.CharField(max_length=50, db_collation="NOCASE")

produces "name" VARCHAR(50) COLLATE NOCASE NOT NULL on SQLite. The collation name is passed through as given so you use whatever the target database knows. Postgres wraps it in double quotes since its collation names are identifiers, the other backends emit it as a bare name.

Motivation and Context

Closes #686. There was no way to ask a text column for a specific collation (like NOCASE on SQLite), you had to drop down to raw SQL. This lets you declare it on the field where it belongs.

How Has This Been Tested?

Added test_schema_db_collation that generates the schema for a model using db_collation on a CharField and a TextField and checks the COLLATE clause shows up. Ran the sqlite test suite locally and everything passes.

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added the changelog accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

Lets a field emit a COLLATE clause in the generated schema, so you can
ask for something like NOCASE on SQLite without dropping to raw SQL.
Defaults to no collation, so existing schemas are unchanged.

Closes tortoise#686
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.

Allow setting collate on fields

1 participant