You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
keyword with causing "column does not exist" #1515
I wanted to make an update query that also returns some more detailed data from other tables. The update query itself works fine, but the "WITH" statement causes the "column does not exist" error during code generation in already tested update query.
Relevant log output
No response
Database schema
createtableattribute_value
(
id bigserialnot null,
val textnot null,
attribute bigintnot null
);
createtableattribute
(
id bigserialnot null,
name textnot null
);
SQL queries
-- name: UpdateAttributeValueDoesntWork :one
with updated_attribute as (UPDATE attribute_value
SET
val = CASE WHEN @filter_value::bool THEN @value ELSE val END
WHEREattribute_value.id= @id
RETURNING id,attribute,val)
selectupdated_attribute.id, val, name
from updated_attribute
left join attribute onupdated_attribute.attribute=attribute.id;
-- name: UpdateAttributeValueWorks :oneUPDATE attribute_value
SET
val = CASE WHEN @filter_value::bool THEN @value ELSE val END
WHEREattribute_value.id= @id
RETURNING id,attribute,val;
@DarkHeros09 hmmm... it could be something else. Its just, the "WITH" query itself is valid and the update query inside it is also valid. Maybe its the combination of database structure and the "WITH" query syntax.
@DarkHeros09 yes I did. The weird part about it is that when I try to SET any other table column instead of "val" with the same query, the code generator
works fine. ( Yes I triple checked the database and the db schema script, also tried to rename the column )
@kubik51 hmm if the query worked on the sql editor, then there's definitely an issue here.
sorry that i can't help you any further, but i hope someone will.
one last thing try using double quotes on val --> "val"
-- name: UpdateAttributeValueDoesntWork :one
WITH updated_attribute AS (
UPDATE attribute_value
SET"val"= CASE WHEN @filter_value::bool THEN @value ELSE "val" END
WHEREattribute_value.id= @id
RETURNING id,attribute,"val")
selectupdated_attribute.id, "val", name
from updated_attribute
left join attribute onupdated_attribute.attribute=attribute.id;
Got a similar issue here:
I've got a definition for a view that makes sqlc fail:
CREATEVIEWpublic.projects_ranked ASSELECTproject_saves.id,
project_saves.name,
project_saves.description,
project_saves.created_at,
project_saves.creator,
project_saves.location,
project_saves.twitter_account,
project_saves.public,
project_saves.last_description_update,
project_saves.save_count,
project_saves.project_id,
update_timestamps.last_update_timestamp,
GREATEST(project_saves.created_at, update_timestamps.last_update_timestamp) AS last_change,
(sqrt((COALESCE(project_saves.save_count, (0)::bigint))::double precision) + COALESCE(((4)::double precision* exp(((('-0.1'::numeric)::double precision* date_part('epoch'::text, age(GREATEST(project_saves.last_description_update, update_timestamps.last_update_timestamp)))) / (((60*60) *24))::double precision))), (0)::double precision)) AS score
FROM ((public.projectsLEFT JOIN ( SELECTcount(*) AS save_count,
project_saves_1.project_idFROMpublic.project_saves project_saves_1
GROUP BYproject_saves_1.project_id) save_counts ON ((projects.id=save_counts.project_id))) project_saves
LEFT JOIN ( SELECTmax(updates."timestamp") AS last_update_timestamp,
updates.project_idFROMpublic.updatesGROUP BYupdates.project_id) update_timestamps ON ((update_timestamps.project_id=project_saves.id)))
WHERE (project_saves.public= true);
Here the "projects" table should have the "id" property (and therefore also "project-saves"), but sqlc generate throws an error:
# package sqlc
schema.sql:178:9: column "id" does not exist
Note that the view itself works perfectly in postgres.
Might this be a similar issue since both have to do with a missing column in a "virtual table" (subselect or view)?
Activity
DarkHeros09 commentedon Mar 28, 2022
Hi @kubik51 i just wrote my own sql query with "WITH" keyword, and it worked as intended no error.
i know its not helpful, but maybe its something else.
konekomind commentedon Mar 28, 2022
@DarkHeros09 hmmm... it could be something else. Its just, the "WITH" query itself is valid and the update query inside it is also valid. Maybe its the combination of database structure and the "WITH" query syntax.
DarkHeros09 commentedon Mar 28, 2022
@kubik51 did you test your query on the database using sql editor before generating it?
konekomind commentedon Mar 28, 2022
@DarkHeros09 yes I did. The weird part about it is that when I try to SET any other table column instead of "val" with the same query, the code generator
works fine. ( Yes I triple checked the database and the db schema script, also tried to rename the column )
DarkHeros09 commentedon Mar 28, 2022
@kubik51 hmm if the query worked on the sql editor, then there's definitely an issue here.
sorry that i can't help you any further, but i hope someone will.
Best of luck.
DarkHeros09 commentedon Mar 28, 2022
one last thing try using double quotes on val --> "val"
konekomind commentedon Mar 29, 2022
@DarkHeros09 tried that too :| still the same error message. Anyway, thank you for your effort.
peteole commentedon Mar 29, 2022
Got a similar issue here:
I've got a definition for a view that makes sqlc fail:
Here the "projects" table should have the "id" property (and therefore also "project-saves"), but
sqlc generatethrows an error:Note that the view itself works perfectly in postgres.
Might this be a similar issue since both have to do with a missing column in a "virtual table" (subselect or view)?
Fix #1515
Fix #1515
kyleconroy commentedon Oct 24, 2023
This is fixed in v1.23.0 by enabling the database-backed query analyzer. We added a test case for this issue so it won’t break in the future.
You can play around with the working example on the playground
test(analyzer): Add more database analyzer test cases (sqlc-dev#2854)