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

populate not working in moleculer-db-adapter-sequelize #296

Open
capcaicah opened this issue Nov 4, 2021 · 10 comments
Open

populate not working in moleculer-db-adapter-sequelize #296

capcaicah opened this issue Nov 4, 2021 · 10 comments
Assignees

Comments

@capcaicah
Copy link

HI, i try use populate to connect my detail to my master, but our populate not display. maybe can you help me

this is my code:

user_detail.js

brokerUser.createService({
  // Define service name
  name: "user_detail",

  mixins: [DbService],

  adapter : new SqlAdapter('node-user', 'root', '', {
            host: 'localhost',
            dialect:'mysql',
          }),
  
  model: {
    name: "user_details",
    define: {
        birth_date: Sequelize.DATE,
    },
    options:{
      timestamps: false,
      createdAt: false,
      updatedAt: false,
    }
  
  }
});

user.js

brokerUser.createService({
  // Define service name
  name: "user",

  mixins: [DbService],

  adapter : new SqlAdapter('node-user', 'root', '', {
            host: 'localhost',
            dialect:'mysql',
          }),
  
  model: {
    name: "users",
    define: {
        name: Sequelize.STRING,
        email: Sequelize.STRING,
    },
    options:{
      timestamps: false,
      createdAt: false,
      updatedAt: false,
    }
  
  },
  settings: {
    populates:{
      'birth_date':'user_detail.get'
    }
  }
});

index.js

brokerUsers.createService({
  // Define service name
  name: "users",

  
  actions:{
    list(ctx){
      return brokerUsers.call('user.list',{populate:['birth_date']});
    }
  }
});
@icebob
Copy link
Member

icebob commented Nov 6, 2021

Please create an exact repro code on repl.it.

@capcaicah
Copy link
Author

hi @icebob this is th repl.it:

https://replit.com/@BothGameplay/-ms-users

@icebob
Copy link
Member

icebob commented Nov 8, 2021

I mean a working example that works on repl.it without transporter and MySQL (use sqlite)

@aheadrox
Copy link

aheadrox commented Mar 24, 2022

Work with the _id field.

@irzhywau
Copy link

irzhywau commented May 7, 2022

@aheadrox What do you mean here exactly?
I'm facing to same issue, using postgresql with this settings in the service

// membership.service
...
settings: {
  fields: ["id", "role", "userId", "communityId", "user"],
  populates: {
    "user": {
      field: "userId",
      action: "users.get",
      params: {
        fields: ["id", "name", "email", "avatar"]
      }
    }
  }
}
...

when calling it I have this piece of code

return ctx.call('membership.find', {
  query: {
    communityId
  },
  populate: ["user"]
});

@irzhywau
Copy link

irzhywau commented May 7, 2022

@icebob I confirm this issue as stated in my previous comment
What I find weird is that when I remove the query section and only let populate in the call args it seems working. but when a query is added into the args the populate doesn't work

With query

mol $ call membership.find '{"populate":["user"], "query": {"communityId": 1}}'
>> Call 'membership.find' with params: { populate: [ 'user' ], query: { communityId: 1 } }
>> Execution time:18ms
>> Response:
[
  {
    id: 1,
    userId: 4,
    communityId: 1,
    role: 'citizen',
  }
]

Without query

mol $ call membership.find '{"populate":["user"]}'
>> Call 'membership.find' with params: { populate: [ 'user' ] }
>> Execution time:2ms
>> Response:
[
  {
    id: 1,
    userId: 4,
    communityId: 1,
    role: 'citizen',
    user: {
      id: 4,
      name: 'Tester',
      email: '[email protected]',
      surname: null,
      avatar: null,
    }
  },
  {
    id: 2,
    userId: 1,
    communityId: 2,
    role: 'citizen',
    user: {
...

And this only works for find method, not working on list and get neither

Any idea?

@icebob
Copy link
Member

icebob commented May 7, 2022

I need a repro example (without any external tools and schemas) what I can debug to investigate the problem.

@irzhywau
Copy link

irzhywau commented May 7, 2022

I think you can use the replit you setup, just need to define 2 different service which are related
My setup contains something else that are not really involved in this issue

@irzhywau
Copy link

irzhywau commented May 7, 2022

@icebob here is a full reproduction https://replit.com/@irzhywau/issue-296
you can run npm run dev to run it

Im still wondering what Im doing wrong, either the library is buggy

@AndreMaz
Copy link
Member

AndreMaz commented May 8, 2022

@irzhywau tested your repro and, after setting the [email protected] to same value as [email protected], got the same result with both call membership.find '{"populate":["user"], "query": {"communityId": 1}}' and call membership.find '{"populate":["user"]}'

image

It seems that user field was properly populated, right? Can you please provide more info about the issue that you're facing?

icebob added a commit that referenced this issue Nov 6, 2022
Fix bug - populate not working - idField mapped to undefined (issue #325, maybe #296)
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

5 participants