Storing year/make/model in a database?

Posted by Mark on Stack Overflow See other posts from Stack Overflow or by Mark
Published on 2010-06-17T02:54:10Z Indexed on 2010/06/17 3:03 UTC
Read the original article Hit count: 300

Filed under:

Here's what I'm thinking (excuse the Django format):

class VehicleMake(Model):
    name = CharField(max_length=50)

class VehicleModel(Model):
    make = ForeignKey(VehicleMake)
    name = CharField(max_length=50)

class VehicleYear(Model):
    model = ForeignKey(VehicleModel)
    year = PositiveIntegerField()

This is going to be used in those contingent drop-down select boxes, which would visually be laid out like [- Year -][- Make -][- Model -]. So, to query the data I need I would first have to select all distinct years from the years table, sorted descending. Then I'd find all the vehicle makes that have produced a model in that year. And then all the models by that make in that year. Is this a good way to do it, or should I re-arrange the foreign keys somehow? Or use a many-to-many table for the years/models so that no year is repeated?

© Stack Overflow or respective owner

Related posts about database-design