-
-
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
SequelizeDatabaseError: missing FROM-clause entry for table "mytablename"
when I use required
#9507
Comments
SequelizeDatabaseError: missing FROM-clause entry for table "mytablename"
when i use requiredSequelizeDatabaseError: missing FROM-clause entry for table "mytablename"
when I use required
Happens to me too. |
Today there was a similar problem, I did |
Example for the second case: https://gist.github.com/PsiXdev/fe26a976bde0893ddeb2d7745ff4fa51 create table "parents" ("_id" serial primary key, "somefield" text not null);
create table "parentToChild" ("parentId" integer not null, "childId" integer not null, "additional" text not null);
create table "children" ("_id" serial primary key); I want to get: { _id: 1,
somefield: 'somevalue',
additionals:
[ { parentId: 1,
childId: 1,
additional: 'some additional info',
child: { _id: 1 } } ] } but I get strange SQL query SELECT "Parent".*,
"additionals"."parentId" AS "additionals.parentId",
"additionals"."childId" AS "additionals.childId",
"additionals"."additional" AS "additionals.additional"
FROM
(SELECT "Parent"."_id",
"Parent"."somefield",
"additionals->child"."_id" AS "additionals.child._id"
FROM "parents" AS "Parent"
INNER JOIN "children" AS "additionals->child" ON "additionals"."childId" = "additionals->child"."_id"
WHERE "Parent"."somefield" = 'somevalue'
AND
(SELECT "parentId"
FROM "parentToChild" AS "additionals"
WHERE ("additionals"."parentId" = "Parent"."_id")
LIMIT 1) IS NOT NULL
LIMIT 1) AS "Parent"
INNER JOIN "parentToChild" AS "additionals" ON "Parent"."_id" = "additionals"."parentId";
-- What?! and SequelizeDatabaseError SequelizeDatabaseError: missing FROM-clause entry for table "additionals" The problem does not occur if I use SELECT "Parent"."_id",
"Parent"."somefield",
"additionals"."parentId" AS "additionals.parentId",
"additionals"."childId" AS "additionals.childId",
"additionals"."additional" AS "additionals.additional",
"additionals->child"."_id" AS "additionals.child._id"
FROM "parents" AS "Parent"
INNER JOIN "parentToChild" AS "additionals" ON "Parent"."_id" = "additionals"."parentId"
INNER JOIN "children" AS "additionals->child" ON "additionals"."childId" = "additionals->child"."_id"
WHERE "Parent"."_id" = 1; The second way to avoid the problem is using SELECT "Parent".*,
"additionals"."parentId" AS "additionals.parentId",
"additionals"."childId" AS "additionals.childId",
"additionals"."additional" AS "additionals.additional",
"additionals->child"."_id" AS "additionals.child._id"
FROM
(SELECT "Parent"."_id",
"Parent"."somefield"
FROM "parents" AS "Parent"
WHERE "Parent"."somefield" = 'somevalue'
AND
(SELECT "parentId"
FROM "parentToChild" AS "additionals"
WHERE ("additionals"."parentId" = "Parent"."_id")
LIMIT 1) IS NOT NULL
LIMIT 1) AS "Parent"
INNER JOIN "parentToChild" AS "additionals" ON "Parent"."_id" = "additionals"."parentId"
INNER JOIN "children" AS "additionals->child" ON "additionals"."childId" = "additionals->child"."_id"; |
You do not consider this a bug? |
I am having this issue as well, using Sequelize 4.38.0 and Postgres 9.6. Adding 'duplicating: true' to the nested required include statement, thank you @psixdev for suggesting that workaround. |
Thanks for various examples, they will eventually help me. Moved to #9869 |
I'm sorry, I've tried using duplicating: false. but, when the data is 8, and I put a limit of: 10.The data taken is only 3 |
Adding duplicating: false to the include breaks limit and offset. |
@rawojtowicz I confirm that adding duplicating = false to the include breaks limit and offset. (It is because a single query is made instead of multiple and therefore the limit is computed before duplicates are removed by sequelize) |
@chrisbag I wish i knew that, haha. I gave up on using sequelize. Afaik this bug exists since almost 8 years. I'm a VERY happy knex user now. :-) |
Any updates on this? |
any update? |
What are you doing?
I have 3 tables:
And I execute this code:
Full code: https://gist.github.com/PsiXdev/09d35df41e8dc9702c4d9c85e3092b87
What do you expect to happen?
I want to get
What is actually happening?
This code generate such sql:
Dialect: postgres
Database version: 9.6
Sequelize version: 4.13.8 / 4.37.10
Examples of changed code with a successful result (click to expand)
I do not fully understand why all these changes work this way. In any case, it seems to me that there is a bug in the library.
The text was updated successfully, but these errors were encountered: