I can’t access from model A to model B and vice versa

(I’m sorry for my English)

Hello everyone!
I have a two lucid models: Image and Folder. I imported Folder.js and Image.js in my controller-file and it works. But when i imported Folder in Image and Image in Folder then i get error-500 “Image.query is not a function”/“Folder.query is not a function”

Okay. I used console.dir for Image-model:

//  ****  from controller.js-file
[Function: Image] {
  '$nestBooted': [ [Circular] ],
  '$hooks': {
    before: Hooks {
      _events: [Array],
      _aliases: [Object],
    after: Hooks {
  '$queryListeners': [],
  '$globalScopes': GlobalScopes { scopes: [] },

//  ****  From Folder.js-file

But why? I imported models everywhere the same:

const Image = use('App/Models/Image.js')

, but it works in controller-file only! Help me, pls

I guess there is a circular dependency problem with your code. You have model A and model B, in model A you are importing model B and in model B you are also importing model A, that’s a recipe for circular dependency. I honestly don’t know if the framework has a an effective way of resolving such problems. But personally, I would rather blame it on bad code organisation than on the framework.

With a problem like this, it’s time you reconsidered your model design. Perhaps, you need to establish some relationships (use of foreign keys) between your models on database level. Consequently, you would not have any need to import the models the way you are currently doing. Check out this link on how to get started with Lucid relationships. It’s quite straightforward