1

Many of the answers to this question advise not to use ordinals, but just to have a single column with the value of the enum when mapping it to a table in DB.

Is this still a safe approach if I'm going to use this enum-mapped table in a many-to-many relationship?

More detailed, I have a table Car and a table Extras, that I'm modelling as an enum. Then I have a table cars_extras, that holds the nxn relationship and has three columns: id, car_id and extra_id, but I'm not sure if this is a good idea.

Community
  • 1
  • 1
user3748908
  • 885
  • 2
  • 9
  • 26

1 Answers1

1

You are enforcing the enumeration in two places: in your code as an enum and in the database as a foreign key.

This will ensure that if someone modifies the data directly in the database, they will not accidentally misspell an enum value, but also makes it easy for the programmer to see the valid values.

I have seen people use this approach and then also add a validation step on startup that ensures that the enum table in the database has the sames values as the enum class. It halts with an error if they don't match.

Jeremy Stein
  • 19,171
  • 16
  • 68
  • 83