-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Find with Include with a where clause generates wrong SQL #3940
Comments
I have also found that the offending code is in "lib/dialects/abstract/query-generator.js" starting in line 1175 where it says:
|
Try adding C with primary key true to the model definition instead. That way sequelize will know what your primary key is and not try to add an id While you can remove the id column after the model has been defined , it is bound to break sooner or later :-) |
The model CD has an id column defined. The problem is that in the inner query that column is referenced as the field, which is "a", and in the outer query the field has been renamed to the model's name "id". As such, using the same filter statement for the join and the where is what is breaking the SQL. SELECT "CD".*,
"cQs"."a" AS "cQs.a",
"cQs"."b" AS "cQs.b",
"cQs"."created_at" AS "cQs.created_at",
"cQs"."updated_at" AS "cQs.updated_at",
"cQs"."c" AS "cQs.c"
FROM ( --INNER QUERY START - inside here there is no "CD"."id"
SELECT "CD"."a" AS "id", --only "CD"."a"
"CD"."ctrd_extra" AS "extra",
"CD"."created_at",
"CD"."updated_at",
"CD"."deleted_at"
FROM "test1" AS "CD"
WHERE "CD"."deleted_at" IS NULL
AND ( SELECT "c"
FROM "test2" AS "cQs"
WHERE "CD"."id" = "cQs"."c" --so this column does not exist here
AND "cQs"."b" = 'bbb'
LIMIT 1
) IS NOT NULL
LIMIT 1
) AS "CD"
INNER
JOIN "test2" AS "cQs"
ON "CD"."id" = "cQs"."c" --OUTER QUERY reference, in here the column does exist
AND "cQs"."b" = 'bbb'; |
Appears to be a bug. Would be great if you can verify whether this persists on the latest version. |
Switched to version 3.2.0 and it still has the same error. Here is the whole log.
|
I am trying to do a find with a nested model with a where clause, as per the example below.
This generates the following SQL for the select, which is wrong because of line 16 :
WHERE "CD"."id" = "cQs"."c"
."CD"."id"
does not exist at that level, it should be"CD"."a"
.Throws the Error: Unhandled rejection SequelizeDatabaseError: column CD.id does not exist.
I am using postgres 9.4 and sequelize 2.1.3.
The text was updated successfully, but these errors were encountered: