tortoise / tortoise/tortoise-orm

Composite primary keys

Open
#1,235 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.6k
Forks
516
Avg merge
2d 21h
Merged PRs (30d)
9

Description

Is your feature request related to a problem? Please describe.
I must create a table where the primary key is a composite of fields, but this is not supported. Excerpt from the docs:

We currently support single (non-composite) primary keys of any indexable field type

Describe the solution you'd like
A model like:

class Mutation(BaseDatasetTag):
    gene: str = fields.CharField(pk=True, max_length=80)
    knock_out: bool = fields.BooleanField(pk=True, default=False)
    knock_in: bool = fields.BooleanField(pk=True, default=False)
    method: str = fields.CharField(pk=True, max_length=200)

Describe alternatives you've considered
There are none, but worse alternatives to this implementation of the table. I will have to use a UUID field, and I will have to filter every time I want to be certain the combinations of field values doesn't exist in my DB.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue provides a proposed Mutation model with multiple fields marked as primary keys, but names no implementation files or tests. Start by tracing how model primary keys and schema generation are handled, then define completion as supporting the requested composite-key model without requiring a separate UUID field and covering the behavior with tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
database
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.