Load model with with association - go

I would like to load the post and is author id and email without loading anything else.
It doesn't need to be eager loading I just need to get all posts and include only the authors id and username
My current query
posts := []interfaces.Post{}
db.Model("User").Find(&posts)
type Post struct{
gorm.Model
Title string
Body string
UserID uint
User User
}
type User struct{
gorm.Model
Username string
Email string
Password string
}
Current response
{
"ID": 1,
"CreatedAt": "2021-01-09T19:11:42.063274-05:00",
"UpdatedAt": "2021-01-09T19:11:42.063274-05:00",
"DeletedAt": null,
"Title": "What does the fox say",
"Body": "whawhhwjg",
"UserID": 1,
"User": {
"ID": 1,
"CreatedAt": "2021-01-09T19:01:28.70267-05:00",
"UpdatedAt": "2021-01-09T19:01:28.70267-05:00",
"DeletedAt": null,
"Username": "12345",
"Email": "1112#gmail.com",
"Password": "$2a$04$T1841Dc52MwjSJ2PaPnTwuFASai6zkGw8WFcuQbO1fi9Nug7R3Iqq"
}
},
Response I'm looking for
{
"ID": 1,
"CreatedAt": "2021-01-09T19:11:42.063274-05:00",
"UpdatedAt": "2021-01-09T19:11:42.063274-05:00",
"DeletedAt": null,
"Title": "What does the fox say",
"Body": "whawhhwjg",
"UserID": 1,
"User": {
"ID": 1,
"Username": "12345",
}
},

You can use the Select method to select the specific fields.
dm.Model("User").Select("ID", "Email", "Username").Find(&posts)
Or, you can use the Preload method like this.
db.Preload("User", func (db *gorm.DB) *gorm.DB {
return db.Select("ID", "Email", "Username")
}).
Find(&posts)

You can also do it in this way
db.Preload("User").Select("ID", "Username").Find(&posts)

Related

How to get particular fields in Preload while using Gorm

I have two models which have many to many relations.
type User struct {
ID uint `gorm:"primary_key" json:"id"`
Name string `json:"name"`
Surname string `json:"surname"`
Email string `json:"email"`
Password string `json:"password"`
RoleID uint `json:"role_id"`
Classes []*Class `gorm:"many2many:user_classes;" json:"classes"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type Class struct {
ID uint `gorm:"primary_key" json:"id"`
Name string `json:"name"`
Homeworks []Homework `gorm:"foreignKey:ClassID" json:"homeworks"`
Materials []Material `gorm:"foreignKey:ClassID" json:"materials"`
Users []*User `gorm:"many2many:classes;" json:"users"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
I want to get users with their classes but with only class id and class name. I did a lot of research and found this solution. However, it didn't work for me. I don't know why. Everything seems fine but didn't work.
My repository function's body is like that
var users []models.User
err := repo.db.Preload("Classes", func(db *gorm.DB) *gorm.DB {
return db.Select("ID", "Name")
}).Find(&users).Error
return users, err
I am expecting results like that
{
"id": 4,
"name": "Furkan",
"surname": "Topaloğlu",
"email": "furkantopalogluu#gmail.com",
"password": "fuki123",
"role_id": 2,
"classes": [
{
"id": 1,
"name": "IELTS Group 1"
},
{
"id": 2,
"name": "Speaking Club Grup 4"
}
],
"created_at": "2022-09-20T02:54:07.185756+03:00",
"updated_at": "2022-09-20T02:54:07.185756+03:00"
}
However, I'm getting this :
{
"id": 4,
"name": "Furkan",
"surname": "Topaloğlu",
"email": "furkantopalogluu#gmail.com",
"password": "fuki123",
"role_id": 2,
"classes": [
{
"id": 1,
"name": "IELTS Group 1",
"homeworks": null,
"materials": null,
"users": null,
"created_at": "0001-01-01T00:00:00Z",
"updated_at": "0001-01-01T00:00:00Z"
},
{
"id": 2,
"name": "Speaking Club Grup 4",
"homeworks": null,
"materials": null,
"users": null,
"created_at": "0001-01-01T00:00:00Z",
"updated_at": "0001-01-01T00:00:00Z"
}
],
"created_at": "2022-09-20T02:54:07.185756+03:00",
"updated_at": "2022-09-20T02:54:07.185756+03:00"
}
What am I doing wrong? Please help me.

Validation of each field in JSON response using karate API

I want to validate particular fields in the response whether it is integer or float(ex: fullbathrooms field). I tried below code but getting match failed error. Could you please help here ?.....Thanks
Given path '/property-client'
And request {"address": <address>,"city": <city>,"state": <state>,"zipCode": <zipCode>}
When method post
Then status 200
And print response
And match response == {fullbathrooms:'#number'}
Examples:
|read('testFile1.csv')|
Error : match failed: EQUALS
Actual response:
{
"success": true,
"message": {
"version": "1.0",
"response": {
"id": "94568859",
"type": "express",
"responseheader": null,
"reportdata": {
"property": {
"source": null,
"type": null,
"dom": null,
"propertytype": "Single Family Residence",
"standardtype": null,
"address": {
"documentid": null,
"number": "150",
"directional": null,
"street": "BRIDGE",
"suffix": "RD",
"postdirectional": null,
"unit": "",
"city": "HILLSBOROUGH",
"state": "CA",
"zip": "94010",
"zipplus4": "6908",
"fulladdress": "150 BRIDGE RD, HILLSBOROUGH, CA 94010"
},
"info": {
"type": null,
"fips": "6081",
"county": "San Mateo",
"bedrooms": "5",
"bathrooms": "6.50",
"fullbathrooms": "6.50",
"totalrooms": "0",
"livingarea": "7750",
"totallivingarea": "7750",
"landarea": "41382",
"landareatype": null,
"pool": "true",
"landvalue": "6904800",
"improvementvalue": "3284414",
"assessedvalue": "10189214",
"assessedyear": "2021",
"taxvalue": "11746898",
"taxyear": "2021",
"deliquentyear": null,
"yearbuilt": "2011",
"propertytax": null,
"approxage": "11",
"parcelnumber": "032-400-110",
"titlecompany": null,
"geocode": {
"latitude": "37.563272",
"longitude": "-122.334442",
"geoqualitycode": ""
}
}
Please take some time to read the documentation: https://github.com/karatelabs/karate#match-contains
I'm not going to refer to your response dump (which by the way is not valid JSON), but give you a simple example. Please pay attention to the structure of your JSON. And note that the 6.50 is a string not a number in your response.
* def response = { "foo": { "bar": { "fullbathrooms": "6.50" } } }
* match response.foo.bar == { fullbathrooms: '#string' }
If you want to validate numbers within strings, please refer other answers, for example: https://stackoverflow.com/search?q=%5Bkarate%5D+number+regex

Relational Database Resulting in Loop

I have the following hierarchy, Users -> Maps -> Elements -> Posts
A user can have a bunch of maps, each map will have a number of elements and each element will have a number of posts.
type User struct {
UserID uint `gorm:"primarykey;autoIncrement;not_null"`
UserName string `json: user_name`
FirstName string `json: first_name`
LastName string `json: last_name`
Email string `json:email`
Password []byte `json:"-"`
Phone string `json:"phone"`
Maps []Map `gorm:"-"`
}
type Map struct {
MapID uint `gorm:"primarykey;autoIncrement;not_null"`
UserID uint `json:userid`
User User `json:"user"; gorm:"foreignkey:UserID`
Title string `json:title`
Desc string `json: "desc"`
Elements []Element `gorm:"foreignKey:MapID"`
Date time.Time `json: date`
}
type Element struct {
ElementID uint `gorm:"primarykey;autoIncrement;not_null"`
ElementName string `json: element_name`
Desc string `json: desc`
MapID uint `json:mapid`
Map Map `json:"map"; gorm:"foreignkey:MapID`
Posts []Post `gorm:"foreignKey:ElementID"`
Date time.Time `json: date`
UserID uint `json:userid`
User User `json:"user"; gorm:"foreignkey:UserID`
}
type Post struct {
PostId uint `gorm:"primarykey;autoIncrement;not_null"`
Title string `json: p_title`
Subject string `json: subject`
Date time.Time `json: date`
Entry string `json: entry_text`
ElementID uint `json:elementid`
Element Element `json:"element"; gorm:"foreignkey:ElementID`
UserID uint `json:userid`
User User `json:"user"; gorm:"foreignkey:UserID`
}
This all seems to work fine, but now when I send the JSON response from the backend there seems to be potential for an infinite loop.
When I retrieve all of a user's maps, it then lists the user object relating to the user that created the map, but the map then also includes a list of elements and within the element object it is going to list the map it belongs to and that map object will again list all of it's elements.
So should I be handling this by just preloading the hierarchy in one direction?
var getmaps []models.Map
database.DB.Preload("User").Preload("Map").Preload("Elements").Offset(offset).Limit(limit).Find(&getmaps)
Or should I be fixing the struct and gorm settings to only get relationships in one direction? Since returning a map will return it's elements and each element will return the map it belongs to which loops back to its elements etc.
This loop would also happen with Elements and posts where an element will have many posts, those post objects would display their element and that element object would display its posts.
I'm sure there is an ideal or optimum way to implement this so just curious what people would recommend.
Example calling one map with the following preloads
func DetailMap(c *fiber.Ctx) error {
id,_ := strconv.Atoi(c.Params("id"))
fmt.Println(id)
var smap models.Map
database.DB.Where("map_id=?", id).Preload("User").Preload("Map").Preload("Elements.Posts").First(&smap)
return c.JSON(fiber.Map{
"data":smap,
})
}
"data": {
"MapID": 1,
"UserID": 1,
"user": {
"UserID": 1,
"UserName": "Chris",
"FirstName": "Chris",
"LastName": "XxxXxxxx",
"Email": "xxxxx#gmail.com",
"phone": "123-456-6789",
"Maps": null
},
"Title": "My Map",
"Desc": "This is the subject",
"Elements": [
{
"ElementID": 1,
"ElementType": "BASE",
"ElementName": "Identity",
"BriefDesc": "This is the identity ",
"Desc": "In publishing and graphic design
"ParentId": "",
"NumElements": 0,
"NumEntries": 0,
"MapID": 1,
"map": {
"MapID": 0,
"UserID": 0,
"user": {
"UserID": 0,
"UserName": "",
"FirstName": "",
"LastName": "",
"Email": "",
"phone": "",
"Maps": null
},
"Title": "",
"Desc": "",
"Elements": null,
"Date": "0001-01-01T00:00:00Z"
},
"Notes": null,
"Questions": null,
"Posts": [
{
"PostId": 1,
"Title": "First Post",
"Subject": "This is the subject",
"Date": "2022-04-11T12:35:55.267-03:00",
"Entry": "This is the Entry",
"ElementID": 1,
"element": {
"ElementID": 0,
"ElementType": "",
"ElementName": "",
"BriefDesc": "",
"Desc": "",
"ParentId": "",
"NumElements": 0,
"NumEntries": 0,
"MapID": 0,
"map": {
"MapID": 0,
"UserID": 0,
"user": {
"UserID": 0,
"UserName": "",
"FirstName": "",
"LastName": "",
"Email": "",
"phone": "",
"Maps": null
},
"Title": "",
"Desc": "",
"Elements": null,
"Date": "0001-01-01T00:00:00Z"
},
"Notes": null,
"Questions": null,
"Posts": null,
"Date": "0001-01-01T00:00:00Z",
"UserID": 0,
"user": {
"UserID": 0,
"UserName": "",
"FirstName": "",
"LastName": "",
"Email": "",
"phone": "",
"Maps": null
}
},
"UserID": 1,
"user": {
"UserID": 0,
"UserName": "",
"FirstName": "",
"LastName": "",
"Email": "",
"phone": "",
"Maps": null
}
}
],
"Date": "2022-04-11T11:31:01.72-03:00",
"UserID": 1,
"user": {
"UserID": 0,
"UserName": "",
"FirstName": "",
"LastName": "",
"Email": "",
"phone": "",
"Maps": null
}
},`
In your Post, Map and Element structs you have the fields:
UserID uint `json:userid`
User User `json:"user"; gorm:"foreignkey:UserID`
You should remove the User field from your content structs because you already have a UserID. A "reference" (ID) in this case is more sensible than including the whole user object. The client can call a /users/{id} endpoint and find more info if needed.
Also limit the content of the User struct by removing Maps []Map (responsible for the loop you mentioned). You would then need to set up endpoints like /user/{id}/maps so the client can get the user's content.
The same applies for Post and Element. You could go all-out and store only IDs, or you can store an array of only "child" models. (Map embeds Element, Element DOES NOT embed Map). So to find the associated map of an element, you would call endpoint /maps/{your element's map ID}. same for Element > Post
type Map struct {
gorm.Model // this takes care of the ID field
UserID uint `json:userid`
Title string `json:title`
Desc string `json: "desc"`
Elements []Element // gorm will handle the relationship automatically
Date time.Time `json: date`
}
type Element struct {
gorm.Model // includes ID
ElementName string `json: element_name`
Desc string `json: desc`
MapID uint `json:mapid`
// Map Map ... This relationship is described by another endpoint - /elements/{elementId}/map to get the related map
Posts []Post // gorm handles this
Date time.Time `json: date`
UserID uint `json:userid`
}
type Post struct {
gorm.Model
Title string `json: p_title`
Subject string `json: subject`
Date time.Time `json: date`
Entry string `json: entry_text`
ElementID uint `json:elementid` // gorm will use this as fk
UserID uint `json:userid`
}
To avoid loops you will need to make the relationships one-directional at a struct level, and set up more http routes to go in the other direction (see commented code).
What I described is a simple REST api. Microsoft has a nice overview: https://learn.microsoft.com/en-us/azure/architecture/best-practices/api-design#organize-the-api-design-around-resources - specifically the customer/order relationship will interest you.
On the gorm side you would be using one-to-many associations: https://gorm.io/docs/has_many.html

Idiomatic way of eager loading - how to populate deep nested association with database/sql and pq

I was just wondering how to deal with eager loading queries to retrieve data with several associations, let me explain my models:
type User struct {
Id uint
Name string
Username string
Image string
}
type Post struct {
Id unit
Content string
Author *User // I use user_id in author column for the posts table
Comments []Comments
}
type Comments struct {
Id unit
PostId uint
Message string
CommentBy *User // I use user_id in comment_by column for the comments table
}
This is an example of retrieved data:
{
"Posts": [
{
"Id": 1,
"Content": "test post",
"Author": {
"Id": 1,
"Name": "author",
"Username": "h3ll0",
"Image": "author.png"
},
"Comments": [
{
"Id": 1,
"PostId": 1,
"Message": "good article",
"CommentBy": {
"Id": 2,
"Name": "second user",
"Username": "r3ader",
"Image": "reader.png"
}
},
{
"Id": 2,
"PostId": 1,
"Message": "bad article",
"CommentBy": {
"Id": 3,
"Name": "third user",
"Username": "thirD",
"Image": "third.png"
}
}
]
}
]
}
I want to retrieve the post data with all nested associations and map them to the Post struct. How would you do it with database/sql and pq (Postgres driver)?
I don't want to use any ORM at all, I only use database/SQL and SQLx.
Keep in mind that the performance is important because The posts and comments tables are big enough.

How to make query result structure match to what i've been declared on GORM Select

I want to make the structure of the query results match what I have stated in GORM Select because right now it only matches the Struct structure. How do i make it work? Thank you in advance
i've tried to make new Struct and it works, but i dont know is it a best practice or not
type User struct {
User_Id uint `json:"user_id" gorm:"column:user_id; PRIMARY_KEY"`
Email string `json:"email"`
Password string `json:"password"`
Token string `json:"token" gorm:"-"`
}
func GetUsers() map[string]interface{} {
users := []User{}
GetDB().Table("app_user").Select("user_id, email").Find(&users)
resp := u.Message(true, "All users")
resp["users"] = users
return resp
}
//actual result
{
"message": "All users",
"status": true,
"users": [
{
"user_id": 1732,
"email": "aaaaaaa#gmail.com",
"password": "",
"token": ""
},
{
"user_id": 1733,
"email": "bbbbbbb#gmail.com",
"password": "",
"token": ""
},
]
}
//Expected result
{
"message": "All users",
"status": true,
"users": [
{
"user_id": 1732,
"email": "aaaaaaa#gmail.com"
},
{
"user_id": 1733,
"email": "bbbbbbb#gmail.com"
}
]
}
It looks like all you need to do is to omit empty fields. You can do that by adding omitempty to json tags:
Password string `json:"password,omitempty"`
Token string `json:"token,omitempty" gorm:"-"`

Resources