Gorm query without gorm.Model columns - go

How can I omit the columns that come from the gorm.Model. E.g. CreatedDate, modifiedDate.
Consider the following model:
type User struct{
gorm.Model
Firstname string
Lastname string
}
Now I can select specific column with:
db.Select("Firstname").Find(&users)
However, while it excludes the Lastname column, it does not exclude the columns that come from the gorm.Model. So the result would look something like:
[{"ID":0,"CreatedAt":"0001-01-01T00:00:00Z","UpdatedAt":"0001-01-01T00:00:00Z","DeletedAt":null,"Firstname":"Foobar"}]
I want only the Firstname column.

You can define your User type without including gorm.Model struct
type User struct{
Firstname string
Lastname string
}
Alternatively, you can use two types, one with gorm.Model and the second without it. You can use the latter when retrieving values without gorm.Model properties.

Related

One-to-many association get data from the one table

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.

Gorm not giving correct inserted record's primary key ID

I am using GORM with GO.
I have an entity User
type User struct {
Id int `json:"id" gorm:"primaryKey;autoIncrement"`
Name string `json:"name"`
gorm.Model
}
My Create record code is
user := User{}
user.Name = "Afzal"
DB.Create(&user)
Now as per Doc user.ID should return inserted data's primary key
But it's returning 0 which is the default value when struct User was initialized.
User.Id will have the correct value, because you've added that field and tagged it as being the primary key. user.ID actually accesses user.Model.ID. This, too, has the primaryKey tag, but it's superseded by the Id field on your User type.
You've embedded the gorm.Model type (as per the docs, I imagine), but that means your User type actually looks like this:
User{
Id int
Name string
gorm.Model{
ID uint `gorm:"primarykey"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt DeletedAt `gorm:"index"`
}
}
So when you look at user.ID, you're accessing the embedded ID field. Either check the Id field (lower-case d), or remove the Id field you've added, and rely on the ID from the embedded Model.
If you do want to keep your own Id field, I think the gorm.Model is right to make it a uint. If you're needing to faff around with negative ID elements in your data, you're probably doing something wrong... I've seen negative ID's being used, but every time I saw it happen, it was some absolutely horrible hack.

How do I use custom preload in gorm

I have these 2 structs
type User struct {
gorm.Model
Name string
CompanyID int
Company Company
}
type Company struct {
gorm.Model
Name string
Adress string
}
I want to get users and preload their companies but I don't want to get the Adress field I tried the custom preload like bellow and I tested it in postman . the query returned all the fields but for adress I get an empty string , the reason why this is happening is that when storing the result in the user struct golang automatically initialize all the fields and the field adress get returned with its initial value which is an empty sting
var user []User
db.Table("users").Preload("Company",func(db *gorm.DB) *gorm.DB {
return db.Select("ID" ,"Name")
}).Find(&user)
what should I do to only have the name and id but not the adress
If your server exports the result as json you could add the json tag omitempty as such:
type Company struct {
gorm.Model
Name string
Adress string `json:"omitempty"`
}
That way the address field will not be included if it is an empty string.

Additional field in a many-to-many relationship in gorm

There are two model use gorm in my project as following:
type User struct {
gorm.Model
Name string `gorm:"type:varchar(128)" json:"name"`
}
type Service struct {
gorm.Model
Name string `gorm:"type:varchar(128)" json:"name"`
Members []*User `gorm:"many2many:user_to_service;" json:"members"`
}
Now, I need to add a field role in the user_to_service, represent the user role in the service such as admin, member, guest and so on.
How do I add the role field in user_to_service? And How to update or query with this field use gorm?

One-to-many recursive relationship in GORM

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"`
}

Resources