Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save struct with same relation in associated table #6985

Open
typomedia opened this issue Apr 22, 2024 · 1 comment
Open

Save struct with same relation in associated table #6985

typomedia opened this issue Apr 22, 2024 · 1 comment
Assignees
Labels
type:question general questions

Comments

@typomedia
Copy link

typomedia commented Apr 22, 2024

Your Question

Inserting the second user Jack returns the result CompanyID 0.

Name: Bob ➜ CompanyID: 1
Name: Jack ➜ CompanyID: 0

What do I have to do so that the user Jack also receives CompanyID 1? Basically, this is clear, because the company Gopher Inc. already exists through the first insert.

But wouldn't it be better for an UNIQUE constraint failed: companies.name to return the corresponding ID? That would significantly reduce the complexity of the queries. The relations are already defined by the structs. Or am I using the wrong method or the method in a wrong way?

package main

import (
	"fmt"
	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
)

type User struct {
	ID        uint   `gorm:"primaryKey"`
	Name      string `gorm:"unique"`
	CompanyID uint
	Company   Company // Belongs to relationship
}

type Company struct {
	ID   uint   `gorm:"primaryKey"`
	Name string `gorm:"unique"`
}

func (user User) String() string {
	return fmt.Sprintf("Name: %s ➜ CompanyID: %d", user.Name, user.Company.ID)
}

func main() {
	db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{})
	if err != nil {
		panic("failed to connect database")
	}

	db.AutoMigrate(&User{}, &Company{})

	user := User{
		Name: "Bob",
		Company: Company{
			Name: "Gopher Inc.",
		},
	}

	db.Save(&user)

	user = User{
		Name: "Jack",
		Company: Company{
			Name: "Gopher Inc.",
		},
	}

	db.Save(&user)

	var result User
	db.Preload("Company").First(&result, 1)
	fmt.Println(result)

	var result2 User
	db.Preload("Company").First(&result2, 2)
	fmt.Println(result2)
}

The document you expected this should be explained

Expected answer

@typomedia typomedia added the type:question general questions label Apr 22, 2024
@typomedia
Copy link
Author

It works as expected, when I replace the following code:

// gorm.io/gorm v1.25.9
// on_conflict.go:42
if onConflict.DoNothing {
		builder.WriteString("DO NOTHING")
	} else {
		builder.WriteString("DO UPDATE SET ")
		onConflict.DoUpdates.Build(builder)
	}

in

builder.WriteString("DO UPDATE SET ")
onConflict.DoUpdates.Build(builder)

It looks like, you can't use DO NOTHING clause in combination with RETURNING in SQLite.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:question general questions
Projects
None yet
Development

No branches or pull requests

2 participants