序列化-错误:“ user_id”列中的空值违反了非空约束

时间:2019-04-14 00:24:19

标签: sequelize.js

我有一个使用续集的模型“ User”和一个“ Set”模型。尝试创建Set时,我不断收到此错误:

“错误:列“ user_id”中的空值违反了非空约束”

我非常清楚地传递了user_id。这是路线。控制台日志显示user_id(按预期)和类型(按预期)。但是我得到了错误。

$ ./bs11
Enter a word: abracadabra
compare(): [ABRACADABRA] <=> [STONE] = -18
compare(): [ABRACADABRA] <=> [STONABLE] = -18
compare(): [ABRACADABRA] <=> [STOMPS] = -18
Key [ABRACADABRA] not found
Enter a word: STOMPS
compare(): [STOMPS] <=> [STONE] = -1
compare(): [STOMPS] <=> [STONABLE] = -1
compare(): [STOMPS] <=> [STOMPS] = 0
Key [STOMPS] found at 0x7fa1e4402a70 [STOMPS]
Enter a word: stona
compare(): [STONA] <=> [STONE] = -4
compare(): [STONA] <=> [STONABLE] = -66
compare(): [STONA] <=> [STOMPS] = 1
Key [STONA] not found
Enter a word: STONABLE
compare(): [STONABLE] <=> [STONE] = -4
compare(): [STONABLE] <=> [STONABLE] = 0
Key [STONABLE] found at 0x7fa1e4402a78 [STONABLE]
Enter a word: stonable
compare(): [STONABLE] <=> [STONE] = -4
compare(): [STONABLE] <=> [STONABLE] = 0
Key [STONABLE] found at 0x7fa1e4402a78 [STONABLE]
Enter a word: stonc
compare(): [STONC] <=> [STONE] = -2
compare(): [STONC] <=> [STONABLE] = 2
Key [STONC] not found
Enter a word: STONE
compare(): [STONE] <=> [STONE] = 0
Key [STONE] found at 0x7fa1e4402a80 [STONE]
Enter a word: stobneage
compare(): [STOBNEAGE] <=> [STONE] = -12
compare(): [STOBNEAGE] <=> [STONABLE] = -12
compare(): [STOBNEAGE] <=> [STOMPS] = -11
Key [STOBNEAGE] not found
Enter a word: STONEBOAT
compare(): [STONEBOAT] <=> [STONE] = 66
compare(): [STONEBOAT] <=> [STONEBOATS] = -83
compare(): [STONEBOAT] <=> [STONEBOAT] = 0
Key [STONEBOAT] found at 0x7fa1e4402a88 [STONEBOAT]
Enter a word: stoneboatatdawn
compare(): [STONEBOATATDAWN] <=> [STONE] = 66
compare(): [STONEBOATATDAWN] <=> [STONEBOATS] = -18
compare(): [STONEBOATATDAWN] <=> [STONEBOAT] = 65
Key [STONEBOATATDAWN] not found
Enter a word: STONEBOATS
compare(): [STONEBOATS] <=> [STONE] = 66
compare(): [STONEBOATS] <=> [STONEBOATS] = 0
Key [STONEBOATS] found at 0x7fa1e4402a90 [STONEBOATS]
Enter a word: zoo
compare(): [ZOO] <=> [STONE] = 7
compare(): [ZOO] <=> [STONEBOATS] = 7
Key [ZOO] not found
Enter a word: ^D
$

这是我的用户模型。我使用的是较旧的语法,但它似乎仍然可以工作……除此之外。

router.post('/create', jsonParser, async (req, res) => {
    console.log("ROUTE", req.body.user_id, typeof req.body.user_id)
    try {
        const set = await Set.create({
            name: req.body.name, 
            user_id: req.body.user_id
        }); 
        return res.status(202).json({ set }); 
    }
    catch(err) {
        console.error(err)
        return res.sendStatus(404)
    }
});

这是Set模型:

const User = sequelize.define('User', 
    {
        id: {
            type: Sequelize.INTEGER, 
            primaryKey: true, 
            autoIncrement: true
        }, 
        username: {
            type: Sequelize.STRING, 
            allowNull: false, 
            unique: true
        }, 
        password: {
            type: Sequelize.STRING, 
            allowNull: false
        }
    }, 
    {
        tableName: 'users', 
        timestamps: false, 
        underscored: true
    }
);

User.associate = function(models) {
    User.hasMany(
        models.Set, 
        {
            as: 'set', 
            foreignKey: { allowNull: false }, 
            onDelete: 'CASCADE'
        }
    )
}

有人可以看到我在这里搞砸吗?

0 个答案:

没有答案