SoftwareBrothers / adminjs-sequelizejs

Sequelizejs adapter for AdminBro
MIT License
27 stars 34 forks source link

Error: There are no resources with given id: "[object Object]" #50

Closed leibowitz closed 2 years ago

leibowitz commented 3 years ago

I am getting errors when dealing with foreign keys defined without a reference.

Let's assume there are two tables, Post and User

User

class User extends Model {
}
User.init({
        id: {
            type: DataTypes.INTEGER,
            field: 'id',
            allowNull: false,
            primaryKey: true,
            autoIncrement: true
        },
        name: {
            type: DataTypes.STRING,
            field: 'name',
            allowNull: false
        }
    }, {
        sequelize,
        schema: 'public',
        modelName: 'User',
        tableName: 'user',
        timestamps: false
    });

Post

Note the references is commented out. This is by design, and is just left there to highlight how it would be defined, if necessary.

    class Post extends Model {
    };
    Post.init({
        id: {
            type: DataTypes.INTEGER,
            field: 'id',
            allowNull: false,
            primaryKey: true,
            autoIncrement: true
        },
        user_id: {
            type: DataTypes.INTEGER,
            field: 'user_id',
//            references: {
//                model: 'user',
//                key: 'id'
//            },
//            onUpdate: 'NO ACTION',
//            onDelete: 'CASCADE',
            allowNull: false
        }
    }, {
        sequelize,
        schema: 'public',
        modelName: 'Post',
        tableName: 'post',
        timestamps: false
    })

Now we define a relationship, which is totally valid in sequelize even if the references property has not been defined.

            User.hasMany(Post, {
              as: 'posts',
              foreignKey: 'user_id'
            });

By doing this, the this.sequelizePath.references?.model in https://github.com/SoftwareBrothers/admin-bro-sequelizejs/blob/8e672b49095c1504782029ee4eb1d8fe9c1a61b4/src/property.ts#L76 is returning an object (even with the return ... as string

I'm using latest version (1.1.1) and I get this when showing what I get from property.reference()

{
  tableName: 'user',
  table: 'user',
  name: 'User',
  schema: 'public',
  delimiter: '.',
  toString: [Function: toString]
}

Which is similar to what addSchema is returning in sequelize, which is what Model.getTableName returns. And model is set to the value returned by getTableName. So the model property is not of type Model in that case.

And the full error:

Error: There are no resources with given id: "[object Object]" This is the list of all registered resources you can use: ....

at AdminBro.findResource (node_modules/admin-bro/lib/admin-bro.js:249:13)
at ApiController.getActionContext (node_modules/admin-bro/lib/backend/controllers/api-controller.js:86:34)
at ApiController.resourceAction (node_modules/admin-bro/lib/backend/controllers/api-controller.js:129:38)
at handler (node_modules/@admin-bro/express/plugin.js:59:52)
at Layer.handle [as handle_request] (node_modules/express/lib/router/layer.js:95:5)
at next (node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (node_modules/express/lib/router/layer.js:95:5)
at node_modules/express/lib/router/index.js:281:22
at param (node_modules/express/lib/router/index.js:354:14)
at param (node_modules/express/lib/router/index.js:365:14)
at param (node_modules/express/lib/router/index.js:365:14)
at Function.process_params (node_modules/express/lib/router/index.js:410:3)
at next (node_modules/express/lib/router/index.js:275:10)
at node_modules/express-formidable/lib/middleware.js:36:7
at IncomingForm.<anonymous> (node_modules/formidable/lib/incoming_form.js:107:9)
leibowitz commented 3 years ago

replacing model by model?.toString() on that line triggers another error as the name includes the schema as well (public in this case):

Error: There are no resources with given id: ""public"."user"" This is the list of all registered resources you can use: ...

at AdminBro.findResource (node_modules/admin-bro/lib/admin-bro.js:249:13)
at PropertyDecorator.reference (node_modules/admin-bro/lib/backend/decorators/property/property-decorator.js:94:36)
at node_modules/admin-bro/lib/backend/utils/populator/populator.js:23:52
at Array.filter (<anonymous>)
at populator (node_modules/admin-bro/lib/backend/utils/populator/populator.js:23:36)
at Object.handler (node_modules/admin-bro/lib/backend/actions/list/list-action.js:89:59)
at processTicksAndRejections (internal/process/task_queues.js:85:5)
at ActionDecorator.handler (node_modules/admin-bro/lib/backend/decorators/action/action-decorator.js:73:19)
at handler (node_modules/@admin-bro/express/plugin.js:59:22)

I tried using model?.tableName instead and that seems to work

github-actions[bot] commented 2 years ago

:tada: This issue has been resolved in version 2.1.2 :tada:

The release is available on:

Your semantic-release bot :package::rocket: