First, I have the Post
model, which has been migrated to the database:
type Post struct {
ID uuid.UUID `gorm:"type:uuid;default:uuid_generate_v4();primary_key"`
UserId uuid.UUID `gorm:"type:uuid;not null"`
User User `gorm:"foreignKey:UserId;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
ParentId *uuid.UUID `gorm:"type:uuid;default:null"`
ParentPost *Post `gorm:"foreignKey:ParentId;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
Content string `gorm:"type:text;not null"`
}
Next, I have a similar model, but with an additional field isLiked, this model is not migrated and is only used to map from Post model to
type PostWithLiked struct {
ID uuid.UUID `gorm:"type:uuid;default:uuid_generate_v4();primary_key"`
UserId uuid.UUID `gorm:"type:uuid;not null"`
User User `gorm:"foreignKey:UserId;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
ParentId *uuid.UUID `gorm:"type:uuid;default:null"`
ParentPost *Post `gorm:"foreignKey:ParentId;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
Content string `gorm:"type:text;not null"`
IsLiked bool
}
This is the function to execute the query
func (r *rPost) GetOne(
ctx context.Context,
id uuid.UUID,
authenticatedUserId uuid.UUID,
) (*entities.PostWithLiked, error) {
var postModel models.PostWithLiked
if err := r.db.WithContext(ctx).
Model(&models.Post{}).
Select(`posts.*,
EXISTS (
SELECT 1
FROM like_user_posts
WHERE like_user_posts.post_id = posts.id AND like_user_posts.user_id = ?
) AS is_liked
`, authenticatedUserId).
Where("posts.id = ?", id).
Preload("User").
Preload("ParentPost.User").
First(&postModel).
Error; err != nil {
return nil, err
}
return mapper.FromPostWithLikedModel(&postModel), nil
}
Here is the query result
[56.799ms] [rows:0] SELECT * FROM "posts" WHERE "posts"."parent_id" = '354fd551-96f0-4899-a7d7-e38ef524636f' AND "posts"."deleted_at" IS NULL
[56.020ms] [rows:1] SELECT * FROM "users" WHERE "users"."id" = '129f1a32-ad18-49f1-b989-b6cd4c243e30' AND "users"."deleted_at" IS NULL
[469.482ms] [rows:1] SELECT posts.*,
EXISTS (
SELECT 1
FROM like_user_posts
WHERE like_user_posts.post_id = posts.id AND like_user_posts.user_id = '7dfdf978-9706-4720-aa3f-382af4b14f70'
) AS is_liked
FROM "posts" WHERE posts.id = '354fd551-96f0-4899-a7d7-e38ef524636f' AND "posts"."deleted_at" IS NULL ORDER BY "posts"."id" LIMIT 1
ParentPost preload is wrong, actually the parent_id of the post is 6d591d06-80ee-4cf3-9695-e1eb986d7885, but it puts the post id in as 354fd551-96f0-4899-a7d7-e38ef524636f to perform preload
when i do the mappingFirst(&models.Post) parent post is preloaded correctly, but when i use PostWithLiked model then preload parentPost uses id of original post instead of parent_id. Please help me.
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question
template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨