Support Forum
Support › MB Relationships › Massive bug found (?) with relationships between posts and users!!!!Resolved
This is my use-case:
A "student" (user) buys a "course" (custom post type) and upon purchase, a relationship is created between the user and the post. This is what grants the user access to the course content.
All has been working wonderfully. Until today.
A course was removed from ALL users who had a relationship with that course! This was NOT supposed to happen.
This happened immediately after I deleted a single user.
Upon further investigation I found that coincidentally the user who was deleted had the exact same ID as the course that went missing for all other users that had a relationship with it.
Upon deleting that particular user, it deleted all rows in the mb_relationships table which pointed to that post/course with the same ID.
To be clear, I deleted a user with the ID of 154 and then any reference in the mb_relationships table to a course with an ID of 154 was removed.
Here is the method in my plugin code which creates the relationship.
protected function register_relationship_for_user_tutorial_access() {
\MB_Relationships_API::register([
'id' => 'the1914_user_to_tutorial',
'from' => [
'object_type' => 'user',
'meta_box' => [
'title' => 'Tutorials',
]
],
'to' => [
'object_type' => 'post',
'post_type' => 'tutorial',
'meta_box' => [
'title' => 'Students',
],
'query_args' => [
'post_parent' => 0, // Show ony parent tutorials
'number' => 100 // Maximum to show in the admin area
]
],
'reciprocal' => true // Prevent metabox showing in the 'to' (tutorial) post type
]);
}
Is this an oversight from the developers of this plugin?
Or is this expected behaviour based on my code above?
I have since set 'reciprical' to FALSE in case that had something to do with it. That does not solve this issue.
Furthermore, when I access the profile of a user to add or remove courses, if the user ID matches a course ID, I get a LOT of dropdown menus appear like this:
Here is a screenshot of the mb_relationships table:
Thank you for your assistance and for an otherwise great set of plugins!
Hi JC,
Thank you very much for your swift response.
You have slightly misunderstood the issue.
The (related) post does not get deleted; that stays intact like you have found.
The issue is ALL other users that had a relationship with that post no longer have a releationship with it.
To replicate the issue:
Once deleted, the remaining two users will no longer have a relationship with the post.
My short-term solution to the issue has been to change the auto-increment of the ID column in the users table to 1 million meaning any new user IDs will be 1M+1, 1M+2, 1M+3, etc, to ensure there will be no more ID collisions between users and posts (my post IDs are still relatively low, less than 200).
That will keep things working on my website for a long time. It's unlikely but if the post IDs in the future exceed 1M, the problem will come back because there will again be user IDs that match post IDs and the problem will persist.
I hope I have made things a little clearer. If you need to know anything else, let me know and I'll be happy to explain further.
Thank you
UPDATE:
This is actually worse than I first thought.
I just deleted a standard page (standard pages have no MB relationships set up on my installation at all).
The page had an ID of 55.
When I then checked the user with an ID of 55, this user no longer has any of the relationships they had before I deleted the completely unrelated page!
This means if I delete ANY post type that happens to share an ID with a user, all of that user's relationships disappear!
This is a very big problem!
Thanks for reading.
Hello, I am bumping this thread because this is a major bug!
Please see my previous post (this one) for instructions about how to replicate this issue.
This is actually worse than I first thought.
I just deleted a standard page (standard pages have no MB relationships set up on my installation at all).
The page had an ID of 55.
When I then checked the user with an ID of 55, this user no longer has any of the relationships they had before I deleted the completely unrelated page!
This means if I delete ANY post type that happens to share an ID with a user, all of that userโs relationships disappear!
This is a very big problem!
Thanks for reading.
I am following this as I am about to set up multiple relationships between post types and terms on a membership site that is going live soon.
Worried about the possible effects of this problem as reported here by JC.
An update from the developers would be appreciated!?
I think you'll be ok unless you are using MB Relationships with the MB User Meta plugin to make relationships between posts and users. If so, you will run into big problems if any of your user IDs share the same IDs with any other posts (of ANY post types).
I too would appreciate an update from the developers because this nearly crippled my website and I would love to see it patched for my own peace of mind and so other developers in the future do not experience what I had to.
I have implemented a hacky workaround as mentioned above BUT it will not last forever:
My short-term solution to the issue has been to change the auto-increment of the ID column in the users table to 1 million meaning any new user IDs will be 1M+1, 1M+2, 1M+3, etc, to ensure there will be no more ID collisions between users and posts (my post IDs are still relatively low, less than 200).
That will keep things working on my website for a long time. It's unlikely but if the post IDs in the future exceed 1M, the problem will come back because there will again be user IDs that match post IDs and the problem will persist.
I meant to say relationships between terms and users, so will be keeping an eye on this thread.
I suspect you will be okay in your scenario because terms utilise the terms database table whereas posts, pages and custom post types utilise the posts table.
Matching IDs from the users table and the posts table is where problems lies.
However, I do not know for sure about terms; you'll have to test this by assigning multiple users to the same term and then deleting any user that shares the ID of said term (or vice versa) to see if everything remains intact as it should.
But still, this is a very significant bug. Developers, please chime in. Here is how you can replicate the issue. Thank you.
Hi friends,
This issue has been fixed on this commit https://github.com/wpmetabox/mb-relationships/commit/bee433a26e25633329b32d6009ce7a8b5f99bcfc
Let me know if it works with your case.
Thanks for responding.
Due to other work commitments I cannot test the updates at the moment. However, I will test these sometime in the next month and get back to you.
Thanks
Apologies for the late reply.
I have installed the latest MB User Meta plugin (v 1.2.9).
I have followed my replication instructions here.
The good news is that when I deleted the user who shared the same ID as the post, all other users who were associated with the post REMAINED associated with the post. (Previously they all lost their relationship with that post!) So this is great, thank you.
However, before I deleted the user who shared the ID with the post. I noticed this:
What I SHOULD be seeing here is this:
The post "TESTING" shares the same ID as the user.
So one bug fixed but another remains:
If the user shares the same ID as the post they are associated with. It doesn't render the user's associations correctly in the user profile
Thanks for your help ๐
Hi,
Thank you for your feedback.
Please assign the setting reciprocal
to true
and re-check this issue. Let me know how it goes.
https://docs.metabox.io/extensions/mb-relationships/#reciprocal-relationships