Is it possible to have multiple values in a single...
# help
j
Is it possible to have multiple values in a single column? I have a "tags" table with a list of potential tags and another table with users and they can have up to 5 tags so prefer to store them under the user table with a column "tags" with values "a,b,c,d,e" for example instead of have to create 5 columns "tag_1" - "tag_5". I tried have the user/tags column link with a foreign key to the tags table but only lets me select 1 tag from the tag table.
t
It’s seems the relationship between user and tag is a many to many.
So you can create another table that is user_tag that has a foreign key to both user and tag table. You can have extra info like tagged_at which could be the time stamp the user created the tag.
An alternative is to use the Postgres array to store a array of tags per user.
j
Thanks, I think I'll have to go the array route, as the tags available to a user will be fixed by what's in the tag table, so I'll just have to reference the tag table when showing the autocomplete dropdown that they can select from and use the tag ids and store as an array in the user table. Does that sound right?
t
yeah that would work.
k
the only problem is: that will make renaming tags hard, and removing a tag from the tags table won't affect users
j
Yes I was wondering about that, at the moment I've setup the tags table to have a boolean column called "restricted" for any tags that should no longer be viewable, so we can "turn them off" instead of deleting. So publicly the restricted tag won't be viewable and if a user goes to update their tags we'll probably set to grey it out with a tooltip that the tag has been removed from the plaform so they can delete and add a new one. We won't rename tags (most likely it would be spelling or capitalization errors that would need fixing), we'd add a new one and restrict the old one if it needs to be completely renamed. Hoping this works, we'll know more once we finish the setup and test the functionality