bun
bun copied to clipboard
Adding NULLS NOT DISTINCT to indexes
I have a scenario where we would like to create an index over some nullable columns. I found out that specifying NULLS NOT DISTINCT on the index in postgres v15+ will have the behavior we want.
I can't figure out how to get the index created using the bun:",unique:group_name". Is there a way to add it using the AfterCreateTable hook?
func (ua UserAssociation) AfterCreateTable(ctx context.Context, query *bun.CreateTableQuery) error {
query.DB().Exec(
"CREATE UNIQUE INDEX ? ON ? (?, ?, ?) NULLS NOT DISTINCT",
bun.Safe("user_associations_pkey"),
bun.Safe("user_associations"),
bun.Safe("user_id"), bun.Safe("organization_id"), bun.Safe("group_id"),
)
// this doesn't work because we can't specify the "NULLS NOT DISTINCT"
//query.DB().NewCreateIndex().Model((*UserAssociation)(nil)).IfNotExists().
// Index("user_associations_unique").
// Column("user_id", "organization_id", "group_id").
// Exec(ctx)
return nil
}