Your Question
The solution I found for https://github.com/go-gorm/gorm/issues/5695 seems to be:
type A struct {
ID uint
MyFavoriteBID uint
MyFavoriteB B `gorm:"foreignKey:MyFavoriteBID;references:ID;"`
MyBs []B
}
type B struct {
ID uint
AID uint
}
However when automigrating I get:
ERROR: relation \"as\" does not exist (SQLSTATE 42P01)
It's because A doesn't exists yet:
CREATE TABLE \"bs\" (\"id\" bigserial,\"a_id\" bigint,PRIMARY KEY (\"id\"),CONSTRAINT \"fk_as_my_bs\" FOREIGN KEY (\"a_id\") REFERENCES \"as\"(\"id\")
It seems to work when enabling DisableForeignKeyConstraintWhenMigrating the first time to create the tables and then run it again without DisableForeignKeyConstraintWhenMigrating the constrains. How can I do this in one go?
The document you expected this should be explained
https://gorm.io/docs/migration.html
Expected answer
A way to create the foreign keys constrains after all the tables are created.