Your Question

I would like AutoMigrate to be able to perform this in my Table2: FOREIGN KEY (table1_id) REFERENCES table1(id) ON DELETE CASCADE but without me having to insert an embedded struct of referenced table inside the current table. In this case, I don't want Table1 object in the Table2 struct:

type Table1 struct { ID uint gorm:"primarykey" Something string gorm:"not null" }

type Table2 struct { ID uint gorm:"primarykey" Thing string gorm:"not null" Table1_id uint gorm:"not null" Table1 Table1 gorm:"foreignKey:Table1_id ;constraint:OnDelete:CASCADE" }

The document you expected this should be explained

https://gorm.io/docs/belongs_to.html

Expected answer

I would expected a simple gorm tag that can be able to do that like:

type Table1 struct { ID uint gorm:"primarykey" Something string gorm:"not null" }

type Table2 struct { ID uint gorm:"primarykey" Thing string gorm:"not null" Table1_id uint gorm:"not null; something_here_that_is_able_to_refer_to_the_ID_in_Table1_and_create_FK_constraint" }

Basically I don't want to make any preload or join to load the record from the other table, I just want the FK there so I can search by the Table1_id and also cascade delete.

Comment From: github-actions[bot]

This issue has been automatically marked as stale because it has been open 360 days with no activity. Remove stale label or comment or this will be closed in 180 days