Left Join weird behavior


I have a table(activetasks) that has a column(truck id) which is a foreign key from the trucks table. I am trying to left join the two tables so that instead of the ID showing up in a display table in my app, the name of the truck shows instead. both tables have a description column. I am using tablename.column to select the data in my query. However, my output is only selecting the trucks.description. I am new to Postgres so it is probably me but any help is greatly appreciated. The unexpected behavior is that the activetasks.description is populated with the trucks.description. So I'm missing one of my description columns.

select activetasks.subject,activetasks.description,activetasks.duedate,activetasks.assignedto,activetasks.status,activetasks.notes,trucks.description from activetasks left join trucks on activetasks.truckid = trucks.id where activetasks.status <> 'Completed'

@SkylarP23, welcome,

My guess is that the task description is being overwritten with the trucks description as they both have the same column name. Try renaming the trucks description column by assigning an alias.

So: …, trucks.description as “truck_description” from activetasks left join …..

1 Like

Hello @mbruijnpff ,

Thank you for your reply. That's what I was guessing too. I just don't understand it because I'm explicitly selecting the table i want the description to come from. Like I said I've never used Postgres before so maybe we chalk it up to that? seems kinda weird though. but I'll try an alias on one of the description fields to see if that works.

Edit: giving an alias on the truck.description worked. Thank you !

1 Like