For example, I want to add a custom user metadata field for projects he is assigned to. But this field must have a foreign key constraint to another table to make sure that the project it is referencing, is valid.
Is there a way to handle this scenario with pure user metadata instead of creating an extra table to link the users with the projects?
For example, I want to add a custom user metadata field for projects he is assigned to. But this field must have a foreign key constraint to another table to make sure that the project it is referencing, is valid.
Is there a way to handle this scenario with pure user metadata instead of creating an extra table to link the users with the projects?
You won’t be able to do this with meta. All user meta is stored in the meta_value
column of wp_usermeta
, so if you tried to set a constraint on that column you’d just prevent any other user meta from being set.
You should be able to store the project id(s) as a string in the wp_usermeta
table, but a constraint won't work since the meta_value
column is used for many different things. Instead, database INSERT and UPDATE triggers could probably be used, though writing correct ones would take more work and thought than just quickly defining a foreign key constraint would.