I need to have an Organization which have relation to parent one. Something like this:
type Organization struct {
gorm.Model
Parent *Organization `gorm:"ForeignKey:ParentId"`
Name string `gorm:"size:30"`
Description string `gorm:"size:100"`
}
I want to have ParentId field which will be referenced to id field in the same table. But as I see there is no field and relation.
How can I fix it ?
I have solved it like this, but I am not sure that it is a correct way:
type Organization struct {
gorm.Model
Parent *Organization
ParentId int `gorm:"TYPE:integer REFERENCES organizations"`
Name string `gorm:"size:30"`
Description string `gorm:"size:100"`
}
Related
Using the gorm.io library, how would I implement a many to many relationship, where the relationship itself has attributes?
Example:
Without the attribute I would do it something like this, but I can't find a solution to my Problem in the GORM docs.
type Recipe struct {
gorm.Model
Name string
Ingredients []Ingredient `gorm:"many2many"`
}
type Ingredient struct {
gorm.Model
Name string
}
I'm new to Golang and Gorm, and I couldn't find the anwswer. In a rest api, I want my json to come with values from the one table relation.
type Product struct {
gorm.Model
Name string
Description string
Weight string
TypeID uint
}
type Type struct {
ID string `gorm:"primaryKey;"`
Name string
Product []Product
}
and I want my Product json to come with the ID and Name from Type.
but this doesn't work.
var product Product
id:=1
db.Preload("Type").First(&product, id)
Do I have to do something like this in the struct?
type Product struct {
gorm.Model
Name string
Description string
Weight string
Type Type
}
If you want to load Type.ID and Type.Name into Product.ID and Product.Name, you need to specifically select fields from two tables:
var product Product
id:=1
db.Joins("JOIN types ON types.id = products.type_id"). Select("types.id, types.name, products.description, products.weight, products.type_id").First(&product, id)
If you want to separate Type fields into a separate field in the Product struct, you need to make the following changes:
type Product struct {
gorm.Model
Name string
Description string
Weight string
TypeID uint
Type Type
}
var product Product
id:=1
db.Preload("Type").First(&product, id)
Here, all of the Type fields will be loaded into the Product.Type field.
I wanted to create relationnal database structure in a using the go package Gorm.
I wanted to add foreign keys to my structure, it works perfectly on regular field, but it does not seem to work when I want to make foreign key for anonymous fields.
Here is some simplified code to demonstrate my case
type StructA struct {
gorm.Model
SimpleFieldID int // appears in database as foreign key
SimpleField StructB
TranslationID int // appears in database as a regular int
Translation
}
type StructB struct {
gorm.Model
SomeField string
SomeOtherField string
}
type Translation struct {
gorm.Model
En string
Fr string
}
I wanted to have anonymous field in the first place to be able to call some GetName() method on any object that has the Translation attribute.
I found this question, but the given anwser was for the has-one relation, where I want to use a belongs-to relation.
I use GORM to build model associations in Golang and I have a struct named Category. A category can have many children categories and it may have a parent category:
type Category struct {
Name string `json:"name"`
Parent Category `json:"parent_category"`
ParentGroupID uint `json:"parent_group_id"`
Children []Category `json:"children_categories"`
}
For this struct, I got an error for Invalid recursive type Category. I checked the GORM documentation but did not find anything helpful there. Any ideas how to model this kind of relation with GORM?
You must declare Parent as *Category (a pointer to Category) instead of Category.
type Category struct {
Name string `json:"name"`
Parent *Category `json:"parent_category"`
ParentGroupID uint `json:"parent_group_id"`
Children []Category `json:"children_categories"`
}
How does compiler know what the size of Parent is. A pointer's size is known, but how big is something that contains itself? (And the inner struct contains itself as well, as does the inner inner struct, and so on.)
Reference: https://stackoverflow.com/a/8261789/4794989
I'm starting with Gorm and trying to model the following:
type MyLink struct {
gorm.Model
Title string
Url string
}
// group of links under a single title
type MyLinkSection struct {
gorm.Model
Title string
Links []MyLink
}
type MyPage struct {
gorm.Model
PageUrl MyLink
Artists []MyLinkSection
}
As you can see I want to be able to refer to the same struct, MyLink as both a foreign keyed object from MyPage but also as a one-to-many from MyLinkSection.
It seems I have to declare the foreign key ID in MyLink which would seem to make this not possible.
Is there any way of setting up tables like this? With a normal DB I'd just have a field in MyPage called my_link_id, with something similar for MyLinkSection.
It seems it is possible to specify forward relations:
PageUrl MyLink `gorm:"ForeignKey:PageUrlId"`
PageUrl Id uint