objection.js icon indicating copy to clipboard operation
objection.js copied to clipboard

ManyToMany relation's require loop

Open abdusamadtv opened this issue 6 years ago • 9 comments

Hi, I have Base model

// models/base.js

const { Model } = require('objection')

class Base extends Model {
  static get modelPaths() {
    return [__dirname]
  }
}

module.exports = Base

Card model

// models/card.js

const Base = require('./base')
const { pg: knex } = require('~root/db')

class Card extends Base {
  static get tableName() {
    return 'cards'
  }

  static get relationMappings() {
    return {
      partners: {
        relation: Base.ManyToManyRelation,
        modelClass: 'partner',
        join: {
          from: 'cards.id',
          through: {
            from: 'partner_card.card_id',
            to: 'partner_card.partner_id',
          },
          to: 'partners.id',
        },
      },
    }
  }

  $beforeUpdate() {
    this.updatedAt = knex.fn.now()
  }
}

module.exports = Card.bindKnex(knex)

Partner model

// models/partner.js

const Base = require('./base')
const { pg: knex } = require('~root/db')

class Partner extends Base {
  static get tableName() {
    return 'partners'
  }

  static get relationMappings() {
    return {
      cards: {
        relation: Base.ManyToManyRelation,
        modelClass: 'card',
        join: {
          from: 'partners.id',
          through: {
            from: 'partner_card.partner_id',
            to: 'partner_card.card_id',
          },
          to: 'cards.id',
        },
      },
    }
  }

  async $beforeUpdate() {
    this.updatedAt = knex.fn.now()
  }
}

module.exports = Partner.bindKnex(knex)

everything seems good but I'm getting this error image

Objection: 2.1.3 Node.js: v12.16.1 OS: macOS Catalina 10.15.3

abdusamadtv avatar Apr 23 '20 08:04 abdusamadtv

this is working when I'm writing like this modelClass: 'Card' or modelClass: 'Partner'

image

but when I'm uploading this to production server (Ubuntu) I'm getting this because my model names are lowercase

image

abdusamadtv avatar Apr 23 '20 09:04 abdusamadtv

Yep, in linux files are case sensitive. You should just write the correct file name in modelClass.

koskimas avatar Apr 23 '20 18:04 koskimas

Yep, in linux files are case sensitive. You should just write the correct file name in modelClass.

@koskimas I'm sorry, but did you see my issue's main message? It's not working even the file name is correct.

abdusamadtv avatar Apr 24 '20 09:04 abdusamadtv

@koskimas Please can you fix this bug? Still not working

image

abdusamadtv avatar May 11 '20 04:05 abdusamadtv

@koskimas could reproduce this bug? I can't understand why are you ignoring this issue? Please help me to solve this problem

abdusamadtv avatar Jun 06 '20 04:06 abdusamadtv

Try to simplify your code until you find where your require loop happens. It doesn't look like being related to objection.

elhigu avatar Jun 08 '20 10:06 elhigu

Try to simplify your code until you find where your require loop happens. It doesn't look like being related to objection.

@elhigu why should I simplify my code if I'm using solution pattern described in the docs?

abdusamadtv avatar Jun 10 '20 03:06 abdusamadtv

@koskimas can you help please? seems like relationMappings method isn't running lazily

abdusamadtv avatar Jun 10 '20 03:06 abdusamadtv

in model file defined modeClass like this:

modelClass: join.path(__direname, 'path/to/model/transaction');

that's will be solve problem !

Dev-El-badry avatar Jan 01 '21 23:01 Dev-El-badry