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

Rel has-many pointer type errors out #513

Open
reesmanp opened this issue Apr 19, 2022 · 3 comments
Open

Rel has-many pointer type errors out #513

reesmanp opened this issue Apr 19, 2022 · 3 comments
Labels
bug Something isn't working stale

Comments

@reesmanp
Copy link

This is using latest Bun on Go 1.17.

I have a has-many relationship on a struct and while trying to select items from it I am receiving this error message *errors.errorString: bun: has-many relation=Items does not have base model=Mission with id=[%!q(*int=0xc0004032a0)] (check join conditions)

The Items struct is defined as:

type Item struct {
    Id                     int          `json:"id" bun:",pk,autoincrement"`
    Name                   string       `json:"name"`
    OwnerId                int          `json:"ownerId"`
    Owner                  User         `json:"owner" bun:"rel:belongs-to,join:owner_id=id"`
    OrganizationId         int          `json:"organizationId"`
    Organization           Organization `json:"organization" bun:"rel:belongs-to,join:organization_id=id"`
    MissionId              *int         `json:"missionId"`
    Mission                *Mission     `json:"mission" bun:"rel:belongs-to,join:mission_id=id"`
}

and the Missions model being:

type Mission struct {
    Id                int           `json:"id" bun:",pk,autoincrement"`
    Name              string        `json:"name"`
    OrganizationId    int           `json:"organizationId"`
    Organization      Organization  `json:"organization" bun:"rel:belongs-to,join:organization_id=id"`
    OwnerId           int           `json:"ownerId"`
    Owner             User          `json:"owner" bun:"rel:belongs-to,join:owner_id=id"`
    VehicleId         int           `json:"vehicleId"`
    Vehicle           Vehicle       `json:"vehicle" bun:"rel:belongs-to,join:vehicle_id=id"`
    Items             []*Item       `json:"items" bun:"rel:has-many,join:id=mission_id"`
}

Items can exist without being assigned to a mission. The query I am trying to run is this:

    missions := new([]models.Mission)
    err := pq.connection.NewSelect().Model(missions).
        Relation("Items").
        Relation("Owner", func(query *bun.SelectQuery) *bun.SelectQuery {
            return query.ExcludeColumn("password").Relation("Owner.Organization")
        }).
        Relation("Organization").
        Relation("Vehicle").
        Relation("Vehicle.Organization").
        Where("mission.organization_id = ?", organizationId).
        Scan(context.Background())

On the Items struct, if I change the pointers to be non-pointers this works. The problem now is that NewInsert does not have an OmitZero method so I have to do this hacky bit:

	var missionId *int
	if item.MissionId > 0 {
		missionId = &item.MissionId
	}
	_, err := pq.connection.NewInsert().Model(item).Value("mission_id", "(?)", missionId).Exec(context.Background())
	return err

Could we get functionality to both include an OmitZero method on NewInsert and allow pointers on rel:belongs-to for rel:has-many relationships?

@vmihailenco vmihailenco added the bug Something isn't working label Apr 20, 2022
@vmihailenco
Copy link
Member

Perhaps you want to add ,nullzero on some columns instead of using pointers, e.g.

type Item struct {
    Id                     int          `json:"id" bun:",pk,autoincrement"`
    Name                   string       `json:"name"`
    OwnerId                int          `json:"ownerId"`
    Owner                  User         `json:"owner" bun:"rel:belongs-to,join:owner_id=id"`
    OrganizationId         int          `json:"organizationId"`
    Organization           Organization `json:"organization" bun:"rel:belongs-to,join:organization_id=id"`
    MissionId              int         `json:"missionId" bun:",nullzero"`
    Mission                *Mission     `json:"mission" bun:"rel:belongs-to,join:mission_id=id"`
}

and allow pointers on rel:belongs-to for rel:has-many relationships

We should add support for this 👍

include an OmitZero method on NewInsert

Most DBMS support DEFAULT instead of omitting values and we already support that with ,nullzero.

@reesmanp
Copy link
Author

reesmanp commented May 2, 2022

The nullzero worked perfectly, thank you

Copy link

github-actions bot commented Nov 7, 2024

This issue has been automatically marked as stale because it has not had activity in the last 30 days. If there is no update within the next 7 days, this issue will be closed.

@github-actions github-actions bot added the stale label Nov 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working stale
Projects
None yet
Development

No branches or pull requests

2 participants