-
-
Notifications
You must be signed in to change notification settings - Fork 861
fix: prevent duplicate table name errors #3216
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
Conversation
e5dd906
to
f643dcb
Compare
I will check failing test next week |
f643dcb
to
8e5e367
Compare
8e5e367
to
c8af15e
Compare
|
Hi @yajra, PR is now ready for review 🙏 |
The changes look good. Will hold off for now and review again in v12 (Laravel 12) release since there is a BC. Will try to complete the v12 within this or earlier next week. Thanks! |
@Seb33300 can you provide an example of the table aliases generated in this PR? One concern I have is the Oracle limitation that only accepts up to 30 chars for object/table name length. Initial Laravel 12 support merged on the master branch. Thanks! |
@yajra It concatenates all parent relation names to create a unique alias name:
Here is an example of query generated (on MySQL): select `users`.*
from `users`
left join `users` as `_updated_by` on `users`.`updated_user_id` = `_updated_by`.`id`
where `_updated_by`.`name` LIKE '%%seb%%'
order by `_updated_by`.`name` asc
limit 10
offset 0 |
Please note that starting Oracle Database 12c Release 2 (12.2) released in 2017, the maximum size is now 128 chars. And all Oracle versions prior that one are already no longer supported by Oracle: |
Yes, unfortunately - I still have projects that uses old Oracle version >.< |
Unless you have columns with multiple joins, It's unlikely to happen, but still theoretically possible. A workaround I can suggest is to use md5 to generate a hash of the alias in case it's more than 30 chars. |
Thanks and will review first on an actual project. I just recalled that we rarely use relation and opt to use join statements for more stable results. |
Tested on a new Laravel 12 app and works as expected. Tested self-join SQL and seems to be fixed: SQL Generated: select * from "users" left join "users" as "_creator" on "users"."created_by" = "_creator"."id" order by "_creator"."name" desc limit 10 offset 0
|
Yes, this is to prevent a potential conflict with a table name if the relation name is the same as the table name. |
Thanks for the feedback. Released on v12 🚀 |
Fixes #2871, #880 (maybe all issues with
Self Join
tag )This PR fixes the duplicate table name SQL error by adding aliases to joined tables.
It also supports multi levels joins:
Eg:
As you can see with the test case I modified, this PR comes with a small BC break.
If a column has defined a custom
order by
usingorderColumn()
, the table is now using an alias that will not match with the table name.A new parameter has been added to the
orderColumn()
callable to pass the column name with alias.