0

Suppose I have a table 'Note':

PK id: int
note: varchar
FK tags: Tags[]

and another table 'Tags":

PK id: int
tag: varchar
FK note: Note[]

Now, if I want to add a new record, say:

{

 "note": "Hello World!, this is an example note",
 "tags": ['first','example']
}

Is there a way to do it in a single request? Or, Do I have to save the tags and make another request with their ids?

Prabin
  • 59
  • 4

1 Answers1

0

If you have a many-to-many relationship, then you should have three tables:

'Note':

PK id: int
note: varchar

'Tags':

PK id: int
tag: varchar

'NoteTags':

note_id int
tag_id int
PK: (note_id, tag_id)

Do three inserts: one insert to Note, one insert to Tags, then one insert to NoteTags for the pair of id's used to associate a given Note to a given Tag.

This is the way to represent a many-to-many relationship. It's not a good idea to store comma-separated lists in a string.

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828