When I select from the table by targeting the station_id column like this:
const getStationById = async ( id ) => {
try {
const station = await pool.query(`SELECT * FROM stations WHERE "station_id" = ${id}`)
return station.rows[0]
} catch (err) {
return err
}
}
It works normally.
But when I target another column like this:
const searchFinName = async (name) => {
try {
const stations = await pool.query(`SELECT station_id, fin_name, swe_name, address, city, operator, capacity, x_coordinate, y_coordinate FROM stations WHERE "fin_name" = ${name})
return station.rows
} catch (err) {
return err
}
}
it returns this error:
{"length":103,"name":"error","severity":"ERROR","code":"42703","position":"43","file":"parse_relation.c","line":"3666","routine":"errorMissingColumn"}.
I have not seen any other solution that helps, can someone please help me identify the problem, thanks.
2
Answers
There does not seem to be a problem with your logic, but rather with syntax or the amount of data being returned too large, which is not very likely.
There is a missing closing quote "`" in your syntax above.
Check that all column names passed in the query are correct.
Try using the following query:
SELECT * FROM stations WHERE ‘fin_name’ = ${name}
As you are already projecting all the columns, using * would be wiser and can prevent the syntax fault that is most likely occurring.
The problem here that postgres parse
$name
variable as a column name not as a string.So, the
$name
variable in your query should be surrounded by single quotes so that postgres can parse it as a string:Alternatively you can pass all the parameters in an array to the
query
function like this: