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

 

posted @ 2019-03-15 10:49  阿泽的小码园  阅读(394)  评论(0编辑  收藏  举报