E
E
entermix2017-04-04 21:32:47
MySQL
entermix, 2017-04-04 21:32:47

How to properly organize has many through with the ability to sort?

There are the following tables, I want to note that this is only an example:

pages
id, name, created, updated

tags
id, name, created, updated

Accordingly, the link table:
pages_tags
page_id, tag_id

Everything would be fine, but in addition you need to be able to sort tags / pages by the date of creation of their connection:
You can do this:
pages_tags
id (автоинкримент), page_id, tag_id

PRIMARY (id, page_id, tag_id), FK (page_id, tag_id)

In this case, by default, sorting occurs by ID, and everything would be fine, but is this correct?
I see several disadvantages, for example:
- It will always be necessary to remove all tags for an article before adding new ones (with a new order), i.e. it turns out that with each change you need to delete / write information again
- this method provokes a turnover of identifiers
Another option:
pages_tags
page_id, tag_id, position

But this is less convenient, and it will not work with ORM, in this case Kohana ORM

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Immortal_pony, 2017-04-04
@Immortal_pony

Everything would be fine, but in addition you need to be able to sort tags / pages by the date of creation of their connection

Perhaps then it is worth adding a column in which this information will be stored?
Why?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question