node.js - (Sequelize+Postgres+NodeJS) Inster FK (userID) from another table -
i'm using sequelize connected postgresql , want use sync function prepare valid models create tables on every environment.
edit:
wallet:
'use strict'; module.exports = function(db, datatypes) { var wallet = db.define('wallet', { id: { type: datatypes.integer, autoincrement: true, allownull: false, primarykey: true }, money: { type: datatypes.integer, allownull: false, defaultvalue: 0 } createdat: datatypes.date, updatedat: datatypes.date }, { tablename: 'wallet', classmethods:{ associate : function( models ) { wallet.belongsto( models.user,{ foreignkey : 'id_user'}); } } }); return wallet; };
user:
'use strict'; var bcrypt = require('bcrypt-nodejs'); var crypto = require('crypto'); var moment = require('moment'); var models = require('./'); var aftercreatehook = function(user, options, fn) { models.wallet.build({id_user: user.id}).save(); fn(null, user); }; module.exports = function(db, datatypes) { var user = db.define('user', { id: { type: datatypes.integer, autoincrement: true, allownull: false, primarykey: true }, password: datatypes.string, email: { type: datatypes.string, unique: true, allownull: false, isemail: true }, login: { type: datatypes.string, unique: true, allownull: false } }, { tablename: 'pl_users', instancemethods: instancemethods, classmethods: { associate : function( models ) { user.hasone( models.wallet); } }, hooks: { aftercreate: aftercreatehook } }); return user; };
why
models.wallet.build({id_user: user.id}).save();
is not working? account creating , want create wallet specific user_id. don't have error...
help please!
you should check relations sequelize docs
in case youu need define:
wallet.belongsto(user,{foreignkey: 'id_user'});
this add id_user wallet model , dont need specify id_user attribute in walletmodel.
if using sequelize import function , should add relation in "associate" class method this:
module.exports = function(db, datatypes) { var wallet = db.define('wallet', { id: { type: datatypes.integer, autoincrement: true, allownull: false, primarykey: true }, pin: { type: datatypes.integer, allownull: false, defaultvalue: 0 }, money: { type: datatypes.string, allownull: false, defaultvalue: 0 }, createdat: datatypes.date, updatedat: datatypes.date }, { tablename: 'wallet', classmethods:{ associate : function( models ) { wallet.belongsto( models.user,{ foreignkey : 'id_user'}); } } });
Comments
Post a Comment