Skip to content
Advertisement

Why does Postgresql function not work as expected

I have table like this

I create get function like this

I tried to call function like this

but return json all 3 row instead of 1 row with booking_id 1467

can you explain root cause ?

Advertisement

Answer

The root cause of you problem is naming your parameter (booking_id) the same as a column name (booking_id) in your table. When processing a query any unqualified reference follows a hierarchy to determine the correct reference; the first being the table column. In this case where booking.booking_status.booking_id = booking_id both references to booking_id are interpreted as referring to the table column. Which is true for every row in the table. You have basically 2 choices: change the parameter name or qualify with the function name.

Also see comments from @a_horse_with_no_name and @AdrianKlaver above.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement