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

Using a similar model to map but preload is wrong #7311

Open
poin4003 opened this issue Dec 10, 2024 · 1 comment
Open

Using a similar model to map but preload is wrong #7311

poin4003 opened this issue Dec 10, 2024 · 1 comment
Assignees
Labels

Comments

@poin4003
Copy link

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.

@github-actions github-actions bot added the type:missing reproduction steps missing reproduction steps label Dec 10, 2024
Copy link

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.ioSearch Before Asking

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants