Matthew Chambers
Matthew Chambers

Reputation: 877

Bad practice to use id on Primary key

I have read that it is bad practice to use naming convention id on primary key. Is this the case or not. Some people online have mentioned it can lead to mistakes when creating joins. Other people have said as long as you are consistent either or is ok- so i could use id to denote id of a property or property_id as long as i am consistent.

Wondering you thoughts/ options people 1) either property_id or id ok for primary key as long as consistent across the board. 2) should use property_id to primary key to avoid making mistakes on joins 3) best to use id- can see which side is primary key on joins and prefix id uneccessary

Upvotes: 1

Views: 675

Answers (1)

Tanzeeb Khalili
Tanzeeb Khalili

Reputation: 7344

I think id is ok. The only legitimate argument against it is in situations like this:

select * from foo, bar where id = 'blah'  # id is ambiguous

In general, when you select from multiple tables, you should qualify every column anyway:

select * from foo, bar where foo.id = 'blah' # no ambiguity

In addition, I think it's better than using model_id for the primary key, since it's customary to use that syntax for foreign keys.

Upvotes: 1

Related Questions