go-gorm / postgres

GORM PostgreSQL driver
MIT License
225 stars 119 forks source link

Cannot create self referencing dependency on none primary key #268

Open efiShtain opened 5 months ago

efiShtain commented 5 months ago

GORM Playground Link

https://github.com/efiShtain/gorm-playground

Description

I'm trying to create a self referencing table where my foreign key points to a unique index which is not the primary key

I suspect that the migrator tries to create the index after the table creation, but the foreign key constraint is created when the table is created, this order of execution cause the issue

I've tried manually changing CreateIndexAfterCreateTable to false in the migrator, but it broke everything, and since it is hard coded to true, I assume there is a good reason for it

Adding the go struct here

type SelfReferencing struct {
    Name         string `gorm:"uniqueIndex;not null"`
    OriginalName *string
    Original     *SelfReferencing `gorm:"references:Name"`
}