Jordi
Jordi

Reputation: 36

Ignore default model attributes in SailsJS for 1 specific model

Hi everyone im stuck at using a model of a specific table of a mysql database. I am using 2 different databases in my SailsJS application. One of the two databases has been created before the SailsJS application, so the tables in this database doesn't have the default attributes configured in config/models.js. This causes an error when I try to call for example the find() function on the model that uses the older database because it misses a column. See following error:

: ER_BAD_FIELD_ERROR: Unknown column 'tbl_user.deleted' in 'field list'

I don't want to add the default attributes to the older database columns, so is it possible to ignore the default attributes configured in config/models.js for specific models?

Upvotes: 0

Views: 604

Answers (1)

Jordi
Jordi

Reputation: 36

After trying a few things i came up with the following solution. Just add the default attributes to your model but add it as an function.

module.exports = {
connection: connection,
migrate: 'safe',
tableName: 'tbl_name',
autoCreatedAt: false,
autoUpdatedAt: false,
autoPK: false,
schema: true,

attributes: {
    id: {
        columnName: 'id',
        type: 'string',
        primaryKey: true
    },
    name: {
        columnName: 'name',
        type: 'string'
    },
    email: {
        columnName: 'email',
        type: 'string'
    },
    deleted: function(){
        var obj = this.toObject();
        delete obj.deleted;
        return obj;

    },

    createdBy: function(){
        var obj = this.toObject();
        delete obj.createdBy;
        return obj;

    }
}
};

In this example the attributes deleted and createdBy are default attributes in config/models.js. I made a function of these attributes in the specific model. In this function i delete this attribute and return the object without the deleted attribute.

Upvotes: 2

Related Questions