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

[DUPLICATE] Relation's owner is not a subclass of Model

Open rewebcan opened this issue 7 years ago • 4 comments

Hello, i searched and implement all suggested solutions but no solution so far. I'am getting this error;

Post.relationMappings.assets: Relation's owner is not a subclass of Model

Can you help me this one please @koskimas ? Here is my models

BaseModel.js

const { Model } = require('objection');
const { DBErrors } = require('objection-db-errors');
const { master } = require('../../db');


class BaseModel extends DBErrors(Model) {
}

BaseModel.knex(master)

module.exports = {
    BaseModel
};

Post.js

const { BaseModel } = require("./BaseModel");

class Post extends BaseModel {

    static get tableName() {
        return 'post';
    }

    static get relationMappings() {
        const Asset = require('./Asset');
        const Customer = require('./Customer');

        return {
            '_customer': {
                relation: BaseModel.BelongsToOneRelation,
                modelClass: Customer,
                join: {
                    from: 'post.id',
                    to: 'customer.id'
                }
            },
            'assets': {
                relation: BaseModel.ManyToManyRelation,
                modelClass: Asset,
                join: {
                    from: 'post.id',
                    through: {
                        from: 'post_asset.post',
                        to: 'post_asset.asset'
                    },
                    to: 'asset.id'
                }
            }
        }
    }
}

module.exports = Post;

Asset.js

const { BaseModel } = require("./BaseModel");

class Asset extends BaseModel {
    static get tableName() {
        return 'asset';
    }
}

module.exports = Asset;

Customer.js

const { BaseModel } = require('./BaseModel');

class Customer extends BaseModel {
    static get tableName() {
        return 'customer';
    }

    static get relationMappings() {
        const CustomerInformation = require('./CustomerInformation');
        const GameProfile = require('./GameProfile');
        return {
            info: {
                relation: BaseModel.HasOneRelation,
                modelClass: CustomerInformation,
                join: {
                    from: 'customer.id',
                    to: 'customerinformation.customer'
                }
            },
            profiles: {
                relation: BaseModel.HasManyRelation,
                modelClass: GameProfile,
                join: {
                    from: 'customer.id',
                    to: 'gameprofile.customer'
                }
            }
        }
    }
}

module.exports = Customer;

LikeConnection.js

const { BaseModel } = require("./BaseModel");

class LikeConnection extends BaseModel {
    static get tableName() {
        return 'likeconnection'
    }
}

module.exports = LikeConnection;

Here is implementation,

const trxResult = await transaction(LikeConnection.knex(), async (trx) => {
                const like = await LikeConnection.query(trx).insert({
                    ...insertData
                });
                await Post.query(trx)
                    .patch({ likeCount: raw('likeCount + 1') })
                    .where('id', postId)

                const post = await Post.query(trx)
                                       .eager('[assets, _customer as customer .[info, profiles]]')
                                       .where('id', postId)
                                       .first()
                return post;
            });

Originally posted by @rewebcan in https://github.com/Vincit/objection.js/issues/693#issuecomment-500385517

rewebcan avatar Jun 10 '19 12:06 rewebcan

Could you create a single .js file that I can debug that reproduces the problem? You can use the reproduction-template.js file as a starting point. My guess at this point is that you somehow have two versions of objection in your setup, which causes the internal instanceof checks to fail.

koskimas avatar Jun 10 '19 13:06 koskimas

We have solved this issue replacing module.exports = Post with module.exports = Post.bindKnex(knexInstance)

10 Haz 2019 Pzt, saat 16:56 tarihinde Sami Koskimäki < [email protected]> şunu yazdı:

Could you create a single .js file that I can debug that reproduces the problem? You can use the reproduction-template.js file as a starting point. My guess at this point is that you somehow have two versions of objection in your setup, which causes the internal instanceof checks to fail.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Vincit/objection.js/issues/1370?email_source=notifications&email_token=ABETKSE22VSFUXWXJ5CEGVTPZZMR3A5CNFSM4HWTAOAKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODXJ6IHI#issuecomment-500425757, or mute the thread https://github.com/notifications/unsubscribe-auth/ABETKSAFJLZH3ROICM2RGPDPZZMR3ANCNFSM4HWTAOAA .

rewebcan avatar Jun 10 '19 14:06 rewebcan

You shouldn't have to do that and it seems more of a hack than a solution.

koskimas avatar Jun 10 '19 14:06 koskimas

Lately, I struggle with your opinion little bit, I think you are right, I cleaned my npm cache, deleted node_modules and package-lock.json, re-run the project, it works.

rewebcan avatar Sep 10 '19 12:09 rewebcan