samedi 15 octobre 2022

Associate metadata tags with other tags and coding references

I have a series of tags that are to be associated to various other entities.

CREATE TABLE `CustomTags` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `tag_name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`))
id tag_name
1 restaurants
2 sunsets
3 business meetings
4 nature
5 snow
6 friendly gatherings
7 winter sports

I have other tables linked with a N:N relationship with this table (i.e. there is an intermediate table that links the ids of each of the table for each relationship). When I load the data I need to be able to add a new tag:

id tag_name
8 winter sports lakes

and be able to:

  1. Load the new tag and associate it with an existing tag conditionally, e.g. if I have a set of other data linked with tag_id 7 then if new data are inserted that are associated with something similar, e.g. 8, I want to be able to group them all as one logical entity i.e. associate <7, winter sports> with <8, winter sports lakes> as in winter sports lakes is an extension of winter sports. And I would like to be able to do it conditionally, e.g. if in the end I want to remove anything associated to tag_id 8 I'd like to be able to do it without other impact.
  2. I'd like to be able in the code to be able to know when I am processing, e.g. restaurants vs business meeting. Originally in the code I used hard-coded id but this doesn't work well because the code is coupled with the ids in the database. The original issue I was trying to avoid is to use hard-coded strings in the code, e.g. restaurants or winter sports that could end up due to a typo to cause a problem in the data. I am not sure how to be able to have the code understand what kind of data it is processing when required.

How can I set my DB tables and definitions in the code to achieve this?

Aucun commentaire:

Enregistrer un commentaire