Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to create Postgres index with "NULLS NOT DISTINCT"? #6085

Open
armoona opened this issue Feb 20, 2023 · 6 comments
Open

How to create Postgres index with "NULLS NOT DISTINCT"? #6085

armoona opened this issue Feb 20, 2023 · 6 comments
Assignees
Labels
type:feature_request feature request

Comments

@armoona
Copy link

armoona commented Feb 20, 2023

How would I create a Postgresql index with NULLS NOT DISTINCT option in gorm?

Tried it with gorm:"index:,option:NULLS NOT DISTINCT", but it didn't work.

See this if you need more context:
https://stackoverflow.com/questions/8289100/create-unique-constraint-with-null-columns

@armoona armoona added the type:question general questions label Feb 20, 2023
@a631807682
Copy link
Member

currently not supported

@a631807682 a631807682 added type:feature_request feature request and removed type:question general questions labels Feb 21, 2023
@philip-madden
Copy link

Any plans to implement this?

@Ponywka
Copy link

Ponywka commented Feb 5, 2024

Just use pointers to make this fields nullable at least it works in my case

type SomethingThird struct {
    ID                int64
    SomethingFirstID  *int64 `gorm:"index:,unique"`
    SomethingSecondID *int64 `gorm:"index:,unique"`
}

@wandering-tales
Copy link

Just use pointers to make this fields nullable at least it works in my case

type SomethingThird struct {
    ID                int64
    SomethingFirstID  *int64 `gorm:"index:,unique"`
    SomethingSecondID *int64 `gorm:"index:,unique"`
}

Did work or not?

@nikolai-momot-thirdfort
index:,unique

It does not work when I tested it locally

@abbyssoul
Copy link

Thank you for raising this issue; I, too, have run into this exact problem.
Here is PR to enable the use of the option param for PG indexes: go-gorm/postgres#288
Note, if I understand the code correctly, the index will only be created if it doesn't exist. So, there is no migration for existing indexes. Thus, when the above PR is merged, it should be possible to specify option:NULLS NOT DISTINCT, but for it to take effect for an existing table, you'll need to delete the existing index first and run a migration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:feature_request feature request
Projects
None yet
Development

No branches or pull requests

8 participants