egg- 配置

1. model

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;
}

猜你喜欢

转载自www.cnblogs.com/czq-0214/p/10535590.html
egg