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

Errors in generated code that implements relationship defined in config. #284

Closed
trentclowater opened this issue Oct 16, 2024 · 8 comments · Fixed by #285 or #286
Closed

Errors in generated code that implements relationship defined in config. #284

trentclowater opened this issue Oct 16, 2024 · 8 comments · Fixed by #285 or #286
Labels
bug Something isn't working

Comments

@trentclowater
Copy link

I have added a relationship in the config file, but it seems to be generating invalid code.

The relationship:

relationships:
  users:
    - name: "users_to_organization_user_roles_through_resource_roles"
      sides:
        - from: "users"
          to: "resource_roles"
          columns: [[id, resource_id]]
          to_where:
            - column: "resource_type"
              sql_value: "users"
              go_value: "users"
        - from: "resource_roles"
          to: "organization_user_roles"
          columns: [[id, resource_role_id]]

This generates the following code in models/organization_user_roles.go:

func (os OrganizationUserRoleSlice) User(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) UsersQuery {
	PKArgs := make([]bob.Expression, len(os))
	for i, o := range os {
		PKArgs[i] = psql.ArgGroup(o.ResourceRoleID)
	}

	return Users.Query(ctx, exec, append(mods,
		sm.InnerJoin(ResourceRoles.NameAs(ctx)).On(
			UserColumns.ID.EQ(ResourceRoleColumns.ResourceID),
			sm.Where(ResourceRoleColumns.ResourceType.EQ(psql.Arg("users"))),
		),
		sm.Where(psql.Group(ResourceRoleColumns.ID).In(PKArgs...)),
	)...)
}

The line

sm.Where(ResourceRoleColumns.ResourceType.EQ(psql.Arg("users")))

causes the error cannot use sm.Where(ResourceRoleColumns.ResourceType.EQ(psql.Arg("users"))) (value of type mods.Where[*dialect.SelectQuery]) as bob.Expression value in argument to sm.InnerJoin(ResourceRoles.NameAs(ctx)).On: mods.Where[*dialect.SelectQuery] does not implement bob.Expression (missing method WriteSQL).

It looks like this line should be:

ResourceRoleColumns.ResourceType.EQ(psql.Arg("users"))

Also in the same file, this code is generated:

func attachOrganizationUserRoleUser1(ctx context.Context, exec bob.Executor, count int, resourceRole1 *ResourceRole, user2 *User) (*ResourceRole, error) {
	setter := &ResourceRoleSetter{
		ResourceType: omit.From(users),

		ResourceID: omit.From(user2.ID),
	}

	err := ResourceRoles.Update(ctx, exec, setter, resourceRole1)
	if err != nil {
		return nil, fmt.Errorf("attachOrganizationUserRoleUser1: %w", err)
	}

	return resourceRole1, nil
}

This causes the error undefined: users. In this case, it looks like the line:

ResourceType: omit.From(users),

should be:

ResourceType: omit.From("users"),

since ResourceType is an omit.Val[string].

There is also a similar problem in models/users.go in another generated function:

func attachUserOrganizationUserRoles0(ctx context.Context, exec bob.Executor, count int, resourceRole1 *ResourceRole, user0 *User) (*ResourceRole, error) {
	setter := &ResourceRoleSetter{
		ResourceType: omit.From(users),

		ResourceID: omit.From(user0.ID),
	}

	err := ResourceRoles.Update(ctx, exec, setter, resourceRole1)
	if err != nil {
		return nil, fmt.Errorf("attachUserOrganizationUserRoles0: %w", err)
	}

	return resourceRole1, nil
}

And also in that same file, there is also this:

func (user0 *User) InsertOrganizationUserRoles(ctx context.Context, exec bob.Executor, resourceRole1 *ResourceRole, related ...*OrganizationUserRoleSetter) error {
	if len(related) == 0 {
		return nil
	}

	resourceRole1, err = attachUserOrganizationUserRoles0(ctx, exec, len(related), resourceRole1, user0)
	if err != nil {
		return err
	}

	organizationUserRoles2, err := insertUserOrganizationUserRoles1(ctx, exec, related, resourceRole1)
	if err != nil {
		return err
	}

	user0.R.OrganizationUserRoles = append(user0.R.OrganizationUserRoles, organizationUserRoles2...)

	return nil
}

which causes the error undefined: err in the line:

resourceRole1, err = attachUserOrganizationUserRoles0(ctx, exec, len(related), resourceRole1, user0)

If I manually correct those errors in the generated code, it seems to work as expected.

@trentclowater
Copy link
Author

Sorry, forgot to mention this is using Postgres, and [email protected]

@stephenafamo
Copy link
Owner

See if the above PR fixes the issue @trentclowater

@trentclowater
Copy link
Author

@stephenafamo This fixes the first error with the sm.Where, but the other errors:

  • omit.From(users) instead of omit.From("users")
  • undefined: err

are still present.

@stephenafamo stephenafamo reopened this Oct 16, 2024
@stephenafamo
Copy link
Owner

stephenafamo commented Oct 16, 2024

  • omit.From(users) instead of omit.From("users")

You are the one to fix this. To represent the string, you have to quote it appropriately in the configuration
go_value: '"users"'
However, I understand the mistake because the documentation is poor.

The go_value is written exactly as it. This is to allow the user write other types, not only strings. For example, to represent time, you would have to write go_value: "time.Time{}".

  • undefined: err

I'll look into this

@stephenafamo
Copy link
Owner

Also, can you share a minimal schema I can use to reproduce this?

@trentclowater
Copy link
Author

I can probably share a minimal schema in a day or two. Will get back to you.

For the omit.From problem, now that you've explained it, I can see how I could probably figured it out with what is in the documentation. But just that little explanation you gave above made it instantly clear.

@stephenafamo
Copy link
Owner

New fixes in #286. Try it out and let me know.

@trentclowater
Copy link
Author

Yes, this fixes the issue. The generated code is now correct. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants