GORM Playground Link

Note: I will update this with the playground link as soon as it's ready. I am having an issue with that repository - https://github.com/go-gorm/playground/issues/494

Description

This code:

type Level struct {
    Id          int64 `gorm:"primaryKey"`
    Sequence    int
    Name        string
}

type LessonRole struct {
    Id         string `gorm:"primaryKey;default:uuid_generate_v4()"`
    LessonId   int64  `gorm:"not null"`
    Lesson     *Lesson
    LevelId    int64 `gorm:"not null"`
    Level      *Level
}

db, err := gorm.Open(postgres.Open(DSN()))
if err != nil {
    panic(err)
}

var lessonRole *LessonRole
result := db.
    Preload("Level").
    First(&lessonRole, "id = ?", "3820c4e0-9f65-11ec-9ff0-aa665a180610")
if result.Error != nil {
    panic(result.Error)
}
fmt.Printf("%+v", lessonRole)
db.Debug().Set("gorm:save_associations", false).Save(lessonRole)

Creates this output:

{
  "Id": "3820c4e0-9f65-11ec-9ff0-aa665a180610",
  "LessonId": 108,
  "Lesson": null,
  "LevelId": 11,
  "Level": {
    "Id": 11,
    "Sequence": 0,
    "Name": "Zero",
  },
}

and these queries:

[5.385ms] [rows:0] INSERT INTO "levels" ("sequence","name","id") VALUES (0,'Zero',11) ON CONFLICT DO NOTHING RETURNING "id"

[15.987ms] [rows:1] UPDATE "lesson_roles" SET "lesson_id"=108,"level_id"=11 WHERE "id" = '3820c4e0-9f65-11ec-9ff0-aa665a180610'

The culprit is the Preload but I thought the entire point of save_associations being set false is it should not ...well ...save the associations. The Level also has a primary key so a bit strange it would do an insert. I feel like I have something setup wrong or maybe there is a bug?

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: tamoyal

@jinzhu Any way to have a look at this? I can't get the playground running

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: 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: a631807682

https://gorm.io/docs/associations.html#Skip-Auto-Create-x2F-Update

Comment From: 0x0elliot

that doesn't solve this. please provide guidance on solving, i will pick this up :)