GORM Playground Link
https://github.com/go-gorm/playground/pull/435
Description
Gorm v1.22.5
For objects with a "has one" or "belongs to" relationship, using the Omit("ObjectName.*") notation will cause an empty record to be inserted into the database. You see the raw sequel:
[1.648ms] [rows:1] INSERT INTO `users` VALUES() ON DUPLICATE KEY UPDATE `id`=`id`
[1.484ms] [rows:1] INSERT INTO `accounts` VALUES() ON DUPLICATE KEY UPDATE `user_id`=VALUES(`user_id`)
This is not great as a) you get empty rows dangling around and b) will fail if you have a non-null constraint or unique constraint.
The docs have the following:
For many2many associations, GORM will upsert the associations before creating the join table references, if you want to skip the upserting of associations, you could skip it
The docs say to use this notation to avoid upserting associations. And while this does specifically call out many2many associations, after looking existing issues reported, it has been suggested to use this notation for other relationship types as well.
Note that using Omit("Reference") will cause mysql to try to insert a null column into the db at the time of creation, which is not desired.
Comment From: jinzhu
refer the comment in the PR.
Comment From: anne-markis
@jinzhu I see no comments in the PR.
Comment From: anne-markis
@jinzhu this one? https://github.com/go-gorm/gorm/pull/5135
It literally tried to fix something that has nothing to do with this bug report and then it was closed with a comment saying as such. I don't understand why this request was closed.