tortoise/tortoise-orm

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

Open

#1641 aperta il 6 giu 2024

Vedi su GitHub
 (3 commenti) (0 reazioni) (0 assegnatari)Python (333 fork)batch import
enhancementgood first issue

Metriche repository

Star
 (3863 star)
Metriche merge PR
 (Merge medio 15g 7h) (9 PR mergiate in 30 g)

Descrizione

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

Guida contributor