Reference CollectionProperty in CollectionProperty

Hi,

lets say I have a PropertyGroup, used in a collection property.

    Scene.FriendsIndex = IntProperty(name = "Index for friends", default = 0)
    Scene.Friends = CollectionProperty(type = people)

class people(PropertyGroup): 
    """A Person can have friends"""
    name : StringProperty(name="Name", default="Settings")
    friend : StringProperty(name="friends Propertygroup here")

So I add people to the Friends collection. Now I want to let people have friends.
One way would be, to have the friend attribute be a StringProperty that contains the name of the Friend. Than A for loop could look inside the collection for the friends name.
Now I have a propblem with duplicated names.

What I would prefer, is a reference to the Collection item instead of its name.
I thought, this would go like this:

    Scene.FriendsIndex = IntProperty(name = "Index for friends", default = 0)
    Scene.Friends = CollectionProperty(type = people)

class people(PropertyGroup): 
    """A Person can have friends"""
    name : StringProperty(name="Name", default="Settings")
    friend : PointerProperty(type = Scene.Friends)

But it seems, it isn’t …

Does anyone of you have an idea? Or a direction where I could look or what my misconception here is.

Honestly, I don’t really understand PointerPropertys yet and right now its a messy try and error adventure to figure this out.

I hope you understand, what I wanted to explain here.

A PropertyGroup can’t reference itself in its own definition, and even less so data in the current file.
I think you’ll have to use your first solution but with integer (or string) separate ID lookup instead of names to avoid duplicate name problems - it’s OK if you don’t have a gazillion people and friends items. You just need to keep track of how many people have been created and increment the id every time you create one.

It would look like this :

class IntPropertyGroup(PropertyGroup):
    # This is a way to store ints in a collection property
    id: IntProperty()

class People(PropertyGroup): 
    id: IntProperty()
    name : StringProperty(name="Name", default="Settings")
    age : IntProperty()
    height : FloatProperty()
    friends: CollectionProperty(type=IntPropertyGroup)
    # etc.
    def get_friends(self, people_collection):
        friends_ids = [friend.id for friend in self.friends]
        return [p for p in people_collection if p.id in friends_ids]

    def add_friend(self, friend, symetric=True):
        if friend.id not in [f.id for f in self.friends]:
            self.friends.add().id = friend.id
        if symetric:  # we can enforce friend relationship to be shared by both parties, in reality it's not always the case ;)
            friend.add_friend(self, symetric=False)

    def remove_friend(self, friend, symetric=True):
        for f in self.friends:
            if f.id == friend.id:
                self.friends.remove(f)
                if symetric: # If someone is not your friend anymore, does it mean you're not their friend anymore ?? :)
                   friend.remove_friend(self, symetric=False)
                break

def register():
    bpy.types.Scene.people_collection = CollectionProperty(type=People)