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

Defing models with foreign entities #7239

Open
Pismice opened this issue Oct 14, 2024 · 7 comments
Open

Defing models with foreign entities #7239

Pismice opened this issue Oct 14, 2024 · 7 comments
Assignees
Labels
type:question general questions

Comments

@Pismice
Copy link

Pismice commented Oct 14, 2024

Your Question

type RealWorkout struct {
	gorm.Model
	TemplateID int
	Template   Workout
	WorkoutID  int
}

I have an entity RealWorkout which possesses 1 Workout which is supposed to act as a Template.
In order to have my code working I have to have 3 fields:

  1. The "real" Workout
  2. The ID of this workout but named WorkoutID
  3. The ID of this workout but named TemplateID

2 is needed because even with a model without WorkoutID I get workout_id field in my DB and I sometimes have to set it using WorkoutID

3 is needed because otherwise at compilation I get the following error:
[error] invalid field found for struct gin-app/misc.RealWorkout's field Template: define a valid foreign key for relations or implement the Valuer/Scanner interface

The document you expected this should be explained

I am not a native english speaker but I did not understand what you excepted from me in this chapter :(

Expected answer

Having all those 3 fields seem very repetitive and error prone, ideally it should be possible to only have Template Workout ? I do not get why I also need an ID field and even more why it has to have specific names, I searched through this and did not find an answer: https://gorm.io/docs/belongs_to.html
Am I doing my model wrong ? What is really needed ? What do I need that ID field even though it is a repetition of my Workout.ID

@Pismice Pismice added the type:question general questions label Oct 14, 2024
@omidfth
Copy link
Contributor

omidfth commented Oct 16, 2024

Hi, you must use Pointer for Template instead of using a value object. for example, use Template *Workout.

@Pismice
Copy link
Author

Pismice commented Oct 17, 2024

Why isnt it that way in the doc ?? https://gorm.io/docs/has_one.html

type User struct {
  gorm.Model
  CreditCard CreditCard
}

type CreditCard struct {
  gorm.Model
  Number string
  UserID uint
}

@Pismice
Copy link
Author

Pismice commented Oct 17, 2024

Hi, you must use Pointer for Template instead of using a value object. for example, use Template *Workout.

And I still have errors when doing this:

type Child struct {
	gorm.Model
	Name string
}

type Parent struct {
	gorm.Model
	Name   string
	Enfant *Child
}

@omidfth
Copy link
Contributor

omidfth commented Oct 19, 2024

You still have errors because:
You don't have any foreign key in the child structure. Add ParantID unit to the child structure.

@Pismice
Copy link
Author

Pismice commented Oct 19, 2024

You still have errors because:

You don't have any foreign key in the child structure. Add ParantID unit to the child structure.

Why not in the parent structure ?

@omidfth
Copy link
Contributor

omidfth commented Oct 20, 2024

Imagine the raw query in SQL:

question:
select parent and parent's child name where ID is one.

answer:
SELECT (p.id, p.name, c.name) from parents p WHERE p.id = 1 INNER JOIN child  c ON c.parent_id = p.id;

the child linked to parent => child has parent_id

Now, if any parent has too many children, you have to use an array of children in the parent structure.

@Pismice
Copy link
Author

Pismice commented Oct 21, 2024

well its the parent that should hold the child_id and not the child that should hold the parent_id. So I dont get your example

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

3 participants