1. modelapp
module.exports = app => { const { INTEGER, STRING, TEXT } = app.Sequelize; const User = app.model.define('User', { id: { type: INTEGER.UNSIGNED,//數據類型 autoIncrement: true,//是否自增 primaryKey: true // 是否主鍵 }, username: { type: STRING(20), allowNull: false //是否爲空 } }, { freezeTableName: true, tableName: 'user' }); User.associate = function() { app.model.User.hasOne(app.model.Info, { foreignKey: 'userId' }); app.model.User.hasMany(app.model.Family, { foreignKey: 'userId', targetKey: 'id' }); } return User; }