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

Incorrect mapping of team_user.team_id to user_id when using string primary keys #34

Open
ramonmalcolm10 opened this issue Dec 7, 2024 · 4 comments

Comments

@ramonmalcolm10
Copy link

When Teams configured to use a string as the primary key, the query appears to incorrectly map team_user.team_id to user_id instead of team_user.user_id. This results in a SQL error:

SQLSTATE[42883]: Undefined function: 7 ERROR: operator does not exist: character varying = bigint LINE 1: ...om "teams" inner join "team_user" on "teams"."id" = "team_us... ^ HINT: No operator matches the given name and argument types. You might need to add explicit type casts. (Connection: pgsql, SQL: select "teams".*, "team_user"."team_id" as "pivot_team_id", "team_user"."user_id" as "pivot_user_id", "team_user"."role_id" as "pivot_role_id", "team_user"."created_at" as "pivot_created_at", "team_user"."updated_at" as "pivot_updated_at" from "teams" inner join "team_user" on "teams"."id" = "team_user"."user_id" where "team_user"."team_id" = 1)

Steps to Reproduce

  1. Configure the teams table to use a string as the primary key.
  2. Attempt to query a relationship involving the team_user pivot table, for example:
$request->user()->allTeams();
  1. Observe that the query tries to map team_user.team_id to user_id instead of team_user.user_id, resulting in the SQL error.

Expected Behavior
The generated query should correctly map:

teams.id to team_user.team_id
users.id to team_user.user_id
Actual Behavior
The generated query incorrectly maps team_user. team_id to user_id, leading to a SQL error when executing the query.

Additional Context
This behavior appears to be incorrect, as it does not align with the expected relationships defined in the schema. Please advise if this is a bug or if additional configuration is needed to resolve this issue when using string primary keys.

@Jurager
Copy link
Owner

Jurager commented Dec 8, 2024

Hi, please provide additional information - what version of the package are you using, and how did you configure the model to work with string keys, setting $incrementing = false; or did you perform any additional actions?

@ramonmalcolm10
Copy link
Author

@Jurager I was using the latest version, the issue is not related to the primary key being string. I did a fresh installation of the package and it generated the same query. It won't throw a exception since the user_id and team_id is both int but nonetheless the query being generated seems incorrect.

select "teams".*, "team_user"."team_id" as "pivot_team_id", "team_user"."user_id" as "pivot_user_id", "team_user"."role_id" as "pivot_role_id", "team_user"."created_at" as "pivot_created_at", "team_user"."updated_at" as "pivot_updated_at" from "teams" inner join "team_user" on "teams"."id" = "team_user"."user_id" where "team_user"."team_id" = 1)

@ramonmalcolm10
Copy link
Author

I am using version 2.4.5, the issue seems to be fix in branch 3.0. Is 3.0 the recommended branch I should be using @Jurager ?

@ramonmalcolm10
Copy link
Author

FYI this is what is causing the issue that was updated in branch 3.0

return $this->belongsToMany(Teams::model('team'), Teams::model('membership'), config('teams.foreign_keys.team_id'), 'user_id')

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

No branches or pull requests

2 participants