Bug adding a Member

I am having a bug adding a member where if I set a group as I add the member I get Uncaught BadMethodCallException: ManyManyList::add() can't be called until a foreign ID is set yet if I edit the Member after they have been added then no problem. This is a bug I have introduced (I tried on a virgin project, works fine). I am going down the Rabbit hole to track the bug down but is there any advice for me. Any clues…

If you could post the code around where this error is occurring, that could help. I’d guess that the Member needs to have been written before it can be added - that seems to be what the error suggests.