Comments (10)
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.
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.
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.
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.
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.
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.
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.
Will close this for now, please reopen it if it is still an issue.
from sequelize-paper-trail.
I had/have the same issue, using the same example sequelize code to load in different model files.
from sequelize-paper-trail.
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)
- Allow the noPaperTrail option to be set via continuation-local-storage
- Exclude attributes per entity HOT 1
- Is this project still ongoing? HOT 5
- feature request: bulkCreate hook
- feature request: beforeBulkUpdate and afterBulkUpdate hooks
- relation "revisions" does not exist HOT 1
- How to capture add and remove associations? HOT 1
- sequelize-typescript support HOT 1
- Support for JSONB HOT 8
- Revisions and RevisionHistory tables are not created HOT 7
- Revisions table breaks with Sequelize 5. HOT 1
- update not creating records in revision table HOT 2
- Syntax error on MySQL for the JSONB data type HOT 4
- User Tracking does not populate associated column HOT 1
- JSON field does not work anymore in MySQL 5.7+ HOT 4
- "Revision.documentId cannot be null", HOT 2
- Usage with TypeORM HOT 2
- `model` and `document` is `null` on revision record HOT 1
- Doesn't track with MySQL
- Documentation for enabling User Tracking HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sequelize-paper-trail.