[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Many-many association on the same model (like user_user) doesn't work #155

Open
FedjaVitko opened this issue Nov 4, 2018 · 6 comments
Open

Comments

@FedjaVitko
Copy link

The document inside the user_user collection only contains one user field. Should it not contain two user fields?

Steps to replicate the issue:

  1. Create two users. (POST /user)
  2. Add the second user as a friend of the first one. (POST /user/{firstUserId}/user) { childId: secondUserId })
  3. Get the friends of the firstUser. (GET /user/{firstUserId}/user)
  4. The response object is empty. No friends get returned.
@JKHeadley
Copy link
Owner

Hi @TheoVitkovskiy, I've been super busy lately but I will try to look into this. In the meantime, would you mind posting your code for the user model?

@FedjaVitko
Copy link
Author
FedjaVitko commented Nov 5, 2018

Hi @TheoVitkovskiy, I've been super busy lately but I will try to look into this. In the meantime, would you mind posting your code for the user model?

Hi @JKHeadley, for this I just used the example from the documentation https://resthapi.com/docs/associations.html. You find it under the Linking Models section. In my specific use case I'm doing the same, but with book sections. Here is my code for the section model.

@FedjaVitko
Copy link
Author

Hi @TheoVitkovskiy, I've been super busy lately but I will try to look into this. In the meantime, would you mind posting your code for the user model?

Hi @JKHeadley, for this I just used the example from the documentation https://resthapi.com/docs/associations.html. You find it under the Linking Models section. In my specific use case I'm doing the same, but with book sections. Here is my code for the section model.

function(mongoose) {
const modelName = 'section';
const Types = mongoose.Schema.Types;

const Schema = new mongoose.Schema({
    book: {
        type: Types.ObjectId,
        ref: "book"
    },
    startCounter: {
        type: Types.Number,
        required: true
    },
    endCounter: {
        type: Types.Number,
        required: true
    }
});

Schema.statics = {
    collectionName: modelName,
    routeOptions: {
        associations: {
            book: {
                type: "MANY_ONE",
                model: "book"
            },
            verses: {
                type: "MANY_MANY",
                model: "verse"
            },
            sections: {
                type: "MANY_MANY",
                model: "section",
                alias: "section",
                linkingModel: "section_section"
            }
        }
    }
};

return Schema;

}

@FedjaVitko FedjaVitko reopened this Nov 5, 2018
@OliS76
Copy link
OliS76 commented Dec 10, 2018

I found the same issue.
Interestingly, when I refer to your example above
try a
GET /user/{secondUserId}/user

In my case, this is where I found the association but from secondUserId to secondUserId.

That looks like a bug to me.

@OliS76
Copy link
OliS76 commented Dec 11, 2018

So, interesting further discovery:
It works when you add embedAssociation: true,
then it adds the association for
GET /user/{secondUserId}/user
and for
GET /user/{firstUserId}/user
to the respective association partner.

Just one note:
API-wise I would have exprected the association to be directed
I.e. only firstUserId -> secondUserId
but it generates an undirected association
firstUserId <-> secondUserId

Maybe I missed that from the documentation ;-)

@JKHeadley
Copy link
Owner

@OliS76 Thanks for the input! This will be the first issue I'll look into when I get a chance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants