3
我使用sequelize-auto從數據庫創建模型。但是我無法使用包含方法來處理關係。我收到以下錯誤使用sequalize sequalize-auto關聯問題
錯誤:student_details_tbl(studentDetails)未關聯到user_tbl!
這是我的模型和數據庫連接。
數據庫連接
var config = require('./config');
var Sequelize = require('sequelize');
var db = new Sequelize(config.dbConn.database, config.dbConn.user, config.dbConn.password,{
dialect : config.dbConn.dialect,
host : config.dbConn.host,
port : config.dbConn.port,
pool : config.dbConn.pool,
define : {
timestamps: false
}
});
db.sync();
db.Sequelize = Sequelize;
module.exports = db;
user_tbl:
module.exports = function(sequelize, DataTypes) {
return sequelize.define('user_tbl', {
id: {
type: DataTypes.INTEGER(11),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
firstName: {
type: DataTypes.STRING,
allowNull: true
},
lastName: {
type: DataTypes.STRING,
allowNull: true
},
loginId: {
type: DataTypes.STRING,
allowNull: true
},
password: {
type: DataTypes.STRING,
allowNull: true
},
created: {
type: DataTypes.DATE,
allowNull: true,
defaultValue: sequelize.literal('CURRENT_TIMESTAMP')
},
modified: {
type: DataTypes.DATE,
allowNull: true,
defaultValue: sequelize.literal('CURRENT_TIMESTAMP')
}
}, {
tableName: 'user_tbl'
});
};
student_details_tbl:
module.exports = function(sequelize, DataTypes) {
return sequelize.define('student_details_tbl', {
id: {
type: DataTypes.INTEGER(11),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
userId: {
type: DataTypes.INTEGER(11),
allowNull: true,
references: {
model: 'user_tbl',
key: 'id'
}
},
barcode: {
type: DataTypes.INTEGER(13),
allowNull: false
},
rollNumber: {
type: DataTypes.STRING,
allowNull: false,
defaultValue: ''
},
created: {
type: DataTypes.DATE,
allowNull: true,
defaultValue: sequelize.literal('CURRENT_TIMESTAMP')
},
modified: {
type: DataTypes.DATE,
allowNull: true,
defaultValue: sequelize.literal('CURRENT_TIMESTAMP')
}
}, {
tableName: 'student_details_tbl'
});
};
如何添加。由於模型是從數據庫創建的。 – Jegan
當我使用它時,我必須在生成後手動將其放入。我認爲他們仍然無法自動完成。 https://github.com/sequelize/sequelize-auto/issues/65 –
你能請分享一些示例代碼 – Jegan