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

Recursive query. #1161

Closed
stigmat4j opened this issue Dec 24, 2013 · 5 comments
Closed

Recursive query. #1161

stigmat4j opened this issue Dec 24, 2013 · 5 comments

Comments

@stigmat4j
Copy link

I use sequelize-postgres npm for my nodejs project. The structure of my db

id: Sequelize.INTEGER,
sponsor: Sequelize.INTEGER,
depth: Sequelize.INTEGER,
path: Sequelize.TEXT,
parents: Sequelize.INTEGER,
cycle: Sequelize.INTEGER
Query for db

WITH RECURSIVE
nodes_cte(id, sponsor, depth, path, parents, cycle) AS (
SELECT tn.id, tn.sponsor,
1 AS depth,
'tn.id' AS path,
ARRAY[tn.id],
false
FROM referrals tn
WHERE tn.sponsor = 0
UNION ALL
SELECT c.id, c.sponsor,
p.depth + 1 AS depth,
(p.path || '->' || c.id),
p.parents || p.id,
c.id = ANY(p.parents)
FROM nodes_cte AS p,
referrals AS c
WHERE c.sponsor = p.id
AND NOT p.cycle
)
SELECT * FROM nodes_cte AS n;

If i make the query through Sequelize - i receive empty array. But when i do it through console i receive normal result.

Help me to solve my problem please.

@mickhansen
Copy link
Contributor

http://sequelizejs.com/docs/latest/usage#raw-queries
Are you using the raw parameter?

@stigmat4j
Copy link
Author

Yes, i using next code
sequelize.query(query, Referrals, {raw: true}).success(function (referrals) {
console.info(referrals)
})

and return is [], for direct query to db with console normal result returns.

@mickhansen
Copy link
Contributor

You cant use 2nd parameter callee with raw afaik, so it sohuld be query(sql, null, {raw: true})

@mickhansen
Copy link
Contributor

Hmm, maybe the issue is that it doesn't identify your query as a SELECT query (we match on the first keyword i believe)

@mickhansen
Copy link
Contributor

sequelize.query(
  query,
  Referrals,
  {raw: true, type: Sequelize.QueryTypes.SELECT}
).success(function (referrals) {
  // ...
})

should work, perhaps with null instead of Referrals. Else please re-open

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

No branches or pull requests

2 participants