Your Question
I have three tables:
type ShipmentProvider struct {
ID uint `gorm:"primaryKey"`
Name string
Code string
}
type Shipment struct {
SuborderID uint `gorm:"primaryKey;autoIncrement:false"`
Suborder Suborder
ShipmentProviderID uint `gorm:"not null;index"`
ShipmentProvider ShipmentProvider
PickupPointID sql.NullString
AddressID sql.NullInt32 `gorm:"index"`
Address Address
LabelID sql.NullInt32 `gorm:"index"`
Label *Label `gorm:"foreignKey:LabelID;references:ShipmentID"`
State ShipmentState
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"`
}
and
type Label struct {
ShipmentID uint `gorm:"primaryKey"`
Shipment Shipment `gorm:"foreignKey:ShipmentID;references:SuborderID"`
ParcelTracker string
ShipmentTracker string
FileData []byte `gorm:"type:bytea"`
}
and migrations defined for them:
database.AutoMigrate(&model.ShipmentProvider{}, &model.Shipment{}, &model.Label{})
When I run to migrate these tables unfortunately I have an error:
ERROR: relation "shipments" does not exist (SQLSTATE 42P01) [21.950ms] [rows:0] CREATE TABLE "labels" ("shipment_id" bigserial,"parcel_tracker" text,"shipment_tracker" text,"file_data" bytea,PRIMARY KEY ("shipment_id"),CONSTRAINT "fk_labels_shipment" FOREIGN KEY ("shipment_id") REFERENCES "shipments"("suborder_id"))
The document you expected this should be explained
https://gorm.io/docs/has_one.html https://gorm.io/docs/belongs_to.html
Expected answer
From what I understood with manually defining foreignKey and references on fields I should be able to do that and have it working, but unfortunately this doesn't work.
Comment From: github-actions[bot]
This issue has been marked as invalid question, please give more information by following the Question template, if you believe there is a bug of GORM, please create a pull request that could reproduce the issue on https://github.com/go-gorm/playground, the issue will be closed in 2 days if no further activity occurs. most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: adhip94
@bgruszka Were you able to address this? Even I have one table already created in postgres DB and I am trying to insert a record in that table and I get the same error.
Comment From: Urento
I have the same issue.
Comment From: rofleksey
I have the same issue =)
Comment From: ilearnio
Same here, happens when two tables realte on each other
Comment From: ByPikod
Same here
Is there any workaround or another valid issue about this topic?
Comment From: mftakhullaziz
After i know this issue, i think java is better than golang