GORM Playground Link
https://github.com/go-gorm/playground/pull/548
Description
I have two tables Item and ItemContent:
type Item struct {
gorm.Model
Logo string `json:"logo" gorm:"not null;type:varchar(50)"`
Contents []ItemContent `json:"contents" gorm:"foreignKey:ItemID"`
}
type ItemContent struct {
gorm.Model
ItemID uint `json:"item_id" gorm:"not null"`
Name string `json:"name" gorm:"not null;type:varchar(50)"`
LanguageCode string `json:"language_code" gorm:"not null;type:varchar(2)"`
}
I tried to insert a new Item and ItemContent into the SQLite database:
item := model.Item{
Logo: "logo",
Contents: []model.ItemContent{
{
LanguageCode: "en",
Name: "name",
},
{
LanguageCode: "ar",
Name: "الاسم",
},
},
}
db.Create(&item)
after creating a new Item I needed to replace the contents with new contents using Association in gorm, I wrote this code for replacing by callingAssociation.Replace method. It should remove the previous contents and set these new contents:
db.Model(&item).Association("Contents").Replace([]model.ItemContent{
{
LanguageCode: "en",
Name: "updated name",
},
{
LanguageCode : "ar",
Name: "الاسم المحدث",
},
{
LanguageCode : "fr",
Name: "le nom",
},
})
but there is an error occurred when I call the Association.Replace code:
sqlite3.Error{
code: 19,
extendedCode: 1299,
systemErrno: golang.org/x/sys/windows.ERROR_SUCCESS (0),
err: NOT NULL constraint failed: vendor_contents.vendor_id
}
after that I opened the sqlite3 database and I find a new 3 rows has been added into item_contents table without removing the previous 2 rows, so there is 5 rows in the table. I expected that the Association.Replace removed the existing 2 rows and add the new 3 rows.
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, 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: MuhmdHsn313
I added the playground examples and test.
Comment From: MuhmdHsn313
@jinzhu any update?
Comment From: black-06
Replace will set old associations's foreign key to null, but you set not null at ItemContent.ItemID.
And Gorm recorded an error on your playground
Remove grom:"not null" to fix it :)
Comment From: black-06
Keep checking errors in your code, such as here
err = DB.Model(&item).Association("Contents").Replace(...)
assert.Nil(t, err)
Comment From: MuhmdHsn313
I think the best approach is to delete the old associations and add new ones. Also, I tried to use Association("Contents").Delete(...) but nothing happened. Also, I tried to use Association("Contents").Clear(), and also nothing happened.
For changing the foreign key to not null, I think this is not an efficient solution, in my case, I should keep the foreign key not null.
Comment From: black-06
Maybe we can perform different deletions on Association, just like Unscoped? @jinzhu
Comment From: MuhmdHsn313
any updates? @black-06 @jinzhu
Comment From: black-06
any updates? @black-06 @jinzhu
Nothing, we need @jinzhu or @a631807682 's advice to avoid ineffective work
Comment From: a631807682
I think it is a very good suggestion to add a similar API to the Association to allow users to choose to delete records or set foreign key to null. In fact, we have many similar issues.
Comment From: WinXaito
I still have this error (NOT NULL constraint failed) with gorm gen.
I set a pointer, like this:
type Directory struct {
Uuid uuid.UUID `gorm:"column:uuid;primaryKey"`
MachineUuid *uuid.UUID `gorm:"column:machine_uuid"`
....
}
And there is no not null in my SQL database (SQLite3).
I tried this with gorm gen and I have the error:
if err := query.MachineConfig.Sources.Model(mc).Replace(mc.Sources...); err != nil { // <- can't use Unscoped(), not available
log.Err(err).Msgf("Replace sources")
return err
}
if err := query.MachineConfig.Save(mc); err != nil {
log.Err(err).Msgf("Update machine config")
return err
}
And I can't use Unscoped(), it's not available...
And I have the version 1.25.1 (this PR #6246 is already included)
Comment From: a631807682
@WinXaito Please create another issue in gorm/gen