https://github.com/demopark/sequelize-docs-Zh-CN
const Sequelize = require('sequelize')
const seq = require('./seq')
// 默认情况下,当未提供表名时,Sequelize 会自动将模型名复数并将其用作表名.
// 这种复数是通过称为 inflection 的库在后台完成的,因此可以正确计算不规则的复数(例如 person -> people).
// 默认会自动创建 id createdAt updatedAt 属性,可以通过配置来设置是否自动创建
const User = seq.define('user', {
userName: { // userName属性
type: Sequelize.STRING, // 字符类型
allowNull: true // 是否能为空,默认为true
},
password: {
type: Sequelize.STRING,
allowNull: true
}
}, {
freezeTableName: true //参数停止 Sequelize 执行自动复数化. 此表名为user
})
const Score = seq.define('score', { // 此表明为scores
mathematics: {
type: Sequelize.INTEGER,
allowNull: true
},
english: {
type: Sequelize.INTEGER,
allowNull: true
},
userId: {
type: Sequelize.INTEGER,
allowNull: true
}
})
// 外键
Score.belongsTo(User, {
foreignKey: 'userId'
})
// 同步
// User.sync() - 如果表不存在,则创建该表(如果已经存在,则不执行任何操作)
// User.sync({ force: true }) - 将创建表,如果表已经存在,则将其首先删除
seq.sync({ force: true }).then(() => {
console.log('sync ok')
}).catch(() => {
console.log('sync err')
})