Coder Social home page Coder Social logo

Comments (10)

nielsgl avatar nielsgl commented on May 24, 2024

Hi @jusefb thanks for the feedback. Did you enable the paper trail for that model? For example, if you have a user model, it should be enabled like this:

'use strict';
module.exports = function(sequelize, DataTypes) {
  var user = sequelize.define('User', {
    email: {
      type: DataTypes.STRING,
      allowNull: false,
      unique: true
    }
  }, {
    classMethods: {
      associate: function(models) {
        // other associations can be defined here
      }
    }
  });

  user.hasPaperTrail();

  return user;
};

from sequelize-paper-trail.

jusefb avatar jusefb commented on May 24, 2024

Apologies for a late response. Yes I have enabled paper trail for the model in question. I was trying to understand where the opt.model.name is set but could not find it.

from sequelize-paper-trail.

nielsgl avatar nielsgl commented on May 24, 2024

When the before and after hooks are added, sequelize automatically sets the right values for instance and opt when the hooks are triggered, so it happens internally.
See http://docs.sequelizejs.com/en/latest/docs/hooks/#sequelizeaddhook-permanent-hook
Can you set debug to true in the options and share the debug statements here, that should tell some more about the object and the model options. This line will print the model.opt values when the after hook is called: https://github.com/nielsgl/sequelize-paper-trail/blob/master/lib/index.js#L241 so look for the opt details after this 'afterHook called' is printed to the console. Also which database are you using?

from sequelize-paper-trail.

jusefb avatar jusefb commented on May 24, 2024

Here is the debug output:

afterHook called
instance: { dataValues:
{ revision: 1,
id: 15,
hospitalisation_id: 7,
treatment_type_id: 1,
operation_mode_id: 1,
oxygen_flow: 1,
start_date: null,
end_date: null,
stop_date: null,
updated_at: Fri Jul 22 2016 23:40:47 GMT+0100 (BST),
created_at: Fri Jul 22 2016 23:40:47 GMT+0100 (BST),
title: null,
duration: null,
status: null,
machine_type_id: null,
user_id: null,
bill_id: null,
billed_item_id: null },
_previousDataValues:
{ hospitalisation_id: undefined,
treatment_type_id: undefined,
operation_mode_id: undefined,
oxygen_flow: undefined,
start_date: undefined,
end_date: undefined,
stop_date: undefined,
revision: undefined },
_changed:
{ hospitalisation_id: true,
treatment_type_id: true,
operation_mode_id: true,
oxygen_flow: true,
start_date: true,
end_date: true,
stop_date: true,
revision: true },
'$modelOptions':
{ timestamps: true,
instanceMethods: {},
classMethods: { associate: [Function] },
validate: {},
freezeTableName: false,
underscored: true,
underscoredAll: false,
paranoid: false,
rejectOnEmpty: false,
whereCollection: { id: '6' },
schema: null,
schemaDelimiter: '',
defaultScope: {},
scopes: [],
hooks:
{ beforeCreate: [Object],
beforeUpdate: [Object],
afterCreate: [Object],
afterUpdate: [Object] },
indexes: [],
name: { plural: 'Treatments', singular: 'Treatment' },
omitNul: false,
sequelize:
{ options: [Object],
config: [Object],
dialect: [Object],
models: [Object],
modelManager: [Object],
connectionManager: [Object],
importCache: [Object],
test: [Object],
queryInterface: [Object] },
uniqueKeys: {},
hasPrimaryKeys: true },
'$options':
{ isNewRecord: true,
'$schema': null,
'$schemaDelimiter': '',
attributes: undefined,
include: undefined,
raw: undefined,
silent: undefined },
hasPrimaryKeys: true,
__eagerlyLoadedAssociations: [],
isNewRecord: false,
context: { delta: [ [Object], [Object], [Object], [Object], [Object] ] } }
opt: { hooks: true,
validate: true,
fields:
[ 'id',
'title',
'start_date',
'end_date',
'stop_date',
'oxygen_flow',
'duration',
'status',
'created_at',
'updated_at',
'revision',
'hospitalisation_id',
'machine_type_id',
'operation_mode_id',
'user_id',
'treatment_type_id',
'bill_id',
'billed_item_id' ],
defaultFields:
[ 'id',
'title',
'start_date',
'end_date',
'stop_date',
'oxygen_flow',
'duration',
'status',
'created_at',
'updated_at',
'revision',
'hospitalisation_id',
'machine_type_id',
'operation_mode_id',
'user_id',
'treatment_type_id',
'bill_id',
'billed_item_id' ],
returning: true }

from sequelize-paper-trail.

nielsgl avatar nielsgl commented on May 24, 2024

Thanks, that's really helpful :) It looks like opt.model.name is undefined because you explicitly specify the name with the singular and plural names. From the logs it looks like you have this in your model specification

name: { plural: 'Treatments', singular: 'Treatment' }

Is there a reason for doings this? Sequelize should be able to handle it by itself.
I'm going to try to replicate this situation and work on a fix this weekend.

from sequelize-paper-trail.

jusefb avatar jusefb commented on May 24, 2024

In my application I load the models automatically from the model directory (this was based on a sequelize example)

fs
      .readdirSync(__dirname)
      .filter(function (file) {
        return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
      })
      .forEach(function (file) {
        var model = sequelize['import'](path.join(__dirname, file));

        db[model.name] = model;
      });

  Object.keys(db).forEach(function (modelName) {
    if (db[modelName].associate) {
      db[modelName].associate(db);
    }
  });

So I am not sure what sets the plural and singular names. I assume it is the sequilize import function. Is there a workaround I could use you think?

from sequelize-paper-trail.

nielsgl avatar nielsgl commented on May 24, 2024

If you want a really quick (and a dirty) fix for it you can do this:
In lib.js, on line 272 where the error is thrown, replace opt.model.name with:

instance['$modelOptions'].name.singular

that should resolve it in your case I think while I look into a better solution today. Let me know if that works!

from sequelize-paper-trail.

nielsgl avatar nielsgl commented on May 24, 2024

Will close this for now, please reopen it if it is still an issue.

from sequelize-paper-trail.

eric-radiant avatar eric-radiant commented on May 24, 2024

I had/have the same issue, using the same example sequelize code to load in different model files.

from sequelize-paper-trail.

eric-radiant avatar eric-radiant commented on May 24, 2024

seems like it would be easy enough for the official code to check if instance['$modelOptions'].name.singular or opt.model.name is what is available.

from sequelize-paper-trail.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.