tortoise / tortoise/tortoise-orm

unique indexes (for unique_together) on nullable columns need to be optionable for nulls not distinct

Open
#1,641 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

assume that i have two models shop, customer. each shop can have only one customer with an unique phone number till deletes it. please pay attention that customer's phone_number is not unique in column. that is unique together with its shop and delete state.

from tortoise import Model, fields

class Base(Model):
  deleted_at = fields.DateTimeField(null=True)
  class Meta:
    abstract = True

class Shop(Base):
  title = fields.CharField(...)

class Customer(Base):
  phone_number = fields.CharField(...) # not used unique here as you expect
  shop = fields.ForeignKeyField('models.Shop', 'customers', fields.CASCADE)
  class Meta:
    unique_together = ('shop_id', 'phone_number', 'deleted_at')

but this simple code is not work as i expected
in postgres that i tested, created index did'nt follow by nulls not distinct
when not follow. unique index considers nulls not equal.
tortoise version : 0.21.3

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

No source file or test is named. Start by tracing how Meta.unique_together is converted into PostgreSQL unique indexes, then inspect the generated index for nullable columns. Done means an option can request NULLS NOT DISTINCT for this constraint and the behavior is covered for the shop, customer, phone_number, and deleted_at example.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgresql, python
Domain
databases
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.