Rails has_many: through & # 8594; Should I keep the primary key column?

I have "author" and "book" tables combined in has_many: through the table "author_book"

As far as I can tell, there is no purpose for the primary key field: id in the table "author_book" ... but before I dedicate this idea, I just wanted to confirm. So, is there a reason for storing the id column in the has_many file: through the table?

Thanks in advance...

+8
ruby ruby-on-rails database-design primary-key has-many-through
source share
3 answers

Save it. Later, you will find that a unique identifier pays off in ways that are not obvious from the beginning.
Unlike other agile development principles, it is better to get data quality elements such as these addressed to the front.

+8
source share

If you ever have something unique to an authorโ€™s book relationship, you'll need an id to set it in this table using the AuthorBook model. In this case, this does not seem like a likely scenario, and you can add it later if you need to.

+3
source share

If this has_many through , it means that author_book is the active writer model, so please leave an id for it. But if you use has_and_belongs_to_many , the id table will not need id http://apidock.com/rails/ActiveRecord/Associations/ClassMethods/has_and_belongs_to_many

+3
source share

All Articles