创建其他模型实例时如何引用现有模型实例?

时间:2018-10-15 08:46:06

标签: node.js sequelize.js

sequelize.js中的新手

我有两个模型:UserOrganization,我正在尝试模拟用户可以是一个组织的管理员,组织只能有一个管理员(一个- belongsTo()关联。

这里是定义模型的方式:

const User = sequelize.define(
    "User",
    {
        firstName: Sequelize.STRING,
        lastName: Sequelize.STRING
    },
    { underscored: true }
);

const Organization = sequelize.define(
    "Organization",
    {
        name: Sequelize.STRING
    },
    { underscored: true }
);

Organization.belongsTo(User, {
    as: "admin",
    foreignKey: {
        allowNull: false
    }
});

这是我想按顺序执行的操作:

  1. 创建用户。
  2. 创建组织,然后将创建的用户设置为管理员。

问题:

  

它不允许我将已经存在的用户指定为admin。

这是我尝试的方法:(通过查看文档和其他示例)
1。

// step 1
await User.create({
    firstName: "John",
    lastName: "Doe"
});

// step 2
const adminToSet = await User.findOne({ where: { firstName: "John" } });
await Organization.create(
    {
        name: "my-awesome-organization",
        User: adminToSet
    },
    {
        include: [{ model: User, as: "admin" }]
    }
);

给我一​​个错误,说'Organization.admin_id cannot be null'

2。

// step 1
await User.create({
    firstName: "John",
    lastName: "Doe"
});

// step 2
const adminToSet = await User.findOne({ where: { firstName: "John" } });
const org = Organization.build({
    name: "my-awesome-organization"
});
org.setAdmin(adminToSet);

给我一​​个错误,说id = id || results && results[0][this.getInsertIdField()];(似乎是mssql->我正在使用的方言)

请帮助!

这是完整的代码段:

const Sequelize = require("sequelize");

const connectionOptions = {
    username: "sa",
    password: "Test@123",
    dialect: "mssql",
    host: "localhost",
    port: 1433,
    operatorsAliases: false,
    benchmark: true,

    pool: {
        max: 5,
        min: 0,
        acquire: 30000,
        idle: 10000
    },
    // Dialect specific options. These will be passed to the db driver
    dialectOptions: {
        encrypt: false
    }
};

const sequelize = new Sequelize(connectionOptions);

const User = sequelize.define(
    "User",
    {
        firstName: Sequelize.STRING,
        lastName: Sequelize.STRING
    },
    { underscored: true }
);

const Organization = sequelize.define(
    "Organization",
    {
        name: Sequelize.STRING
    },
    { underscored: true }
);

Organization.belongsTo(User, {
    as: "admin",
    foreignKey: {
        allowNull: false
    }
});

const createUserAndOrganization = async () => {
    // step 1
    await User.create({
        firstName: "John",
        lastName: "Doe"
    });

    // step 2
    const adminToSet = await User.findOne({ where: { firstName: "John" } });
    const org = Organization.build({
        name: "my-awesome-organization"
    });
    org.setAdmin(adminToSet);
};

const authenticated = async () => {
    try {
        await sequelize.sync({ force: true });
        await createUserAndOrganization();
    } catch (e) {
        console.error(e);
    }
};

sequelize
    .authenticate()
    .then(authenticated)
    .catch(err => {
        console.log(`[${err.name}]`, `[${err.original.code}]`, `${err.original.message}`);
    });

0 个答案:

没有答案
相关问题