wpdb - How to work with constraints on wordpress user metadata?

admin2025-06-02  2

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?

Share Improve this question asked Mar 6, 2019 at 14:01 HimadHimad 5552 silver badges9 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748813168a313935.html

最新回复(0)