-
-
Notifications
You must be signed in to change notification settings - Fork 2
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
DatabaseTests fails on SQL Server #13
Comments
Ping @alexweissman since it's originally your code 😬 |
Might be a solution : https://stackoverflow.com/a/55889981/445757 |
I was going to post that suggestion earlier, but had to run to my son soccer game. Using a CTE would likely be the solution, it's a lifesaver when working with Oracle as well. Though you may be able to get away without using a CTE. I don't have MSSQL installed on my home PC, but if I understand the query correctly, this may work:
|
Hum, The result should be:
FYI, you can run SQL Server from Docker : https://hub.docker.com/_/microsoft-mssql-server |
Will try it later, but I just noticed I left off one of your join conditions, try:
|
Nope. I did found something interesting. This code : $user->permissions()
->withCount('roles')
->orderBy('roles_count', 'desc')
->toSql(); Return this SQL:
Which returns this when run on the db :
But when I do this : $user->permissions()
->withCount('roles')
->orderBy('roles_count', 'desc')
->get()
->toArray(); Only 3 results are returned in the array, not 4... Laravel probably account for the duplicate somehow... The error only happens when doing pagination on
When
Producing this (failling) SQL :
Remove the
|
You beat me to the punch. I just noticed I had the other join on the wrong query. But I think I know where I went wrong.
Of course there is no row_number in MySQL, but if you do that and add |
Problem is, once the correct SQL is found, it will need to be converted to the correct Eloquent code.
👀 sprinkle-core/app/src/Database/Relations/Concerns/Unique.php Lines 354 to 356 in 6df7706
Problem is this removes duplicates after the query, but when you limit the query, it needs to be done BEFORE, otherwise I wonder if in this case, the limit EDIT: |
Ha, the # is a comment in MySQL, remove them for MSSQL |
Not quite, but closer |
I've added SQL Server to the test workflow, and I get stuck on this issue. So I'm sharing in case someone as an idea and because writing things down sometimes helps me figure things out.
Actual problem is this: https://stackoverflow.com/a/13999903/445757
It doesn't affect MySQL because MySQL is smart enough to understand how to handle that (somehow). And it's a "fake" issue in our case, because they are really the same slug (same id).
The failed test is this one :
sprinkle-core/app/tests/Integration/Database/DatabaseTests.php
Line 743 in 631fc77
But it's not the query that cause the error. The error is caused by this query :
sprinkle-core/app/src/Database/Relations/Concerns/Unique.php
Line 299 in 631fc77
Here's the "beautified" SQL:
Problematic part is this :
[permissions].*,
. Remove the wildcard, remove theslug
from columns.Failed Solution n° 1
We could replace all existing columns (except the required "id" for the group by):
sprinkle-core/app/src/Database/Relations/Concerns/Unique.php
Line 282 in 631fc77
...but this removes
roles_count
and mess up the sort.There's no way I know to remove
*
without removing theroles_count
(or other user defined select).Failed Solution n° 2
The issue is with GROUP BY, so we remove the group by right?
Here's the result without Group By and limit :
So it does work with Limit 1, but Limit 2 won't work (
testBelongsToManyThroughPaginated
test will fail in this case highlighting this).Group could be done on the Collection, but this means Limit and Offset also needs to be done In the Collection. Would make the whole thing less efficient.
Failed Solution n° 3
DISTINCT
instead ofGROUP BY
doesn't help too.It does fix
testBelongsToManyThroughPaginatedWithOrderByAggregateColumn
, but it doesn't fixtestBelongsToManyThroughPaginated
:Which returns ID
[2, 2]
instead of[2, 3]
Original SQL :
Other solutions
We could "Cheat" the test and force select only 'id' :
But
slug
is not part of the results anymore (and bug is still there).So I'm bit stuck now. SQL Server doesn't support this
Linked Branch : https://github.com/userfrosting/sprinkle-core/tree/5.0-mssql
Other references:
The text was updated successfully, but these errors were encountered: