如何强制与 Sequelizejs 进行 1:n 关联 [英] How to force 1:n association with Sequelizejs

查看:11
本文介绍了如何强制与 Sequelizejs 进行 1:n 关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实施一些测试以确保我的续集对象被正确保存.我有一个非常简单的架构:文章 <--> 用户

I'm implementing some tests to make sure my sequelize objects are saved correctly. I have a very simple schema: Article <--> Users

文章ONE 用户
发布用户可以发布许多 文章

这是我的文章模型定义:

Here is my Article model definition:

module.exports = function(sequelize){
    "use strict";

    var Sequelize = require('sequelize');
    ...
    var Article = sequelize.define("Article", {
        slug: {
            type: Sequelize.STRING,
            unique: true,
            comment: "Unique URL slug to access the article"
        },
        title: {
            type: Sequelize.STRING,
            unique: true,
            allowNull: false,
            validate: {
                notEmpty: true
            }
        },
        summary: {
            type: Sequelize.TEXT,
            allowNull: true
        },
        body: {
            type: Sequelize.TEXT,
            allowNull: true
        },
        published: {type: Sequelize.BOOLEAN, allowNull: false, defaultValue: true},
        allowComment: {type: Sequelize.BOOLEAN, allowNull: false, defaultValue: true}
    }, {
        freezeTableName: true,
        classMethods: {
            associate: function (models)
            {
                Article.belongsTo(models.User, {as: "Author", foreignKey: 'author_id'});
                Article.hasMany(models.Comment, {as: "Comments", foreignKey: 'article_id'});
            },
            articlesForIndex: function()
            {
                return this.findAll({
                    where: {published: true},
                    order: 'createdAt DESC',
                    limit: 10
                });
            }
        },
        setterMethods   : {
            title : function(v) {
                this.setDataValue('title', v.toString());
                this.setDataValue('slug', slugify(v));
            }
        }
    });

    return Article;
};

我想要做的是强制 Article 有一个 Author (User).使用当前定义,我可以在没有 Author 的情况下创建文章.

What I want to do is forcing the Article to have an Author (User). With the current definition I can create Article without Author.

这是我失败的测试:

module.exports = function (sequelize, models) {
    'use strict';

    var Q = require('q');
    var should = require('chai').should();

    describe('Article Model', function () {

        describe('Validation', function () {

            ...

            it('should not be valid without an author', function (done) {
                models.Article.create({title: 'title5', summary: 'summary', body: 'body'})
                    .should.be.rejected.notify(done);
            });
        });
    });
};

推荐答案

在最近(我相信是 2.0-rc2)版本的 sequelize 中,您可以将外键更改为对象:

On fairly recent (2.0-rc2 i believe) versions of sequelize you can change the foreign key to an object:

Article.belongsTo(User, {
    as: "Author", 
    onDelete: 'CASCADE', 
    foreignKey: { name:'author_id', allowNull: false }
});

您还需要添加 onDelete: 'CASCADE' 因为我们不能再在删除时设置 null

You also need to add onDelete: 'CASCADE' because we can no longer set null on delete

这篇关于如何强制与 Sequelizejs 进行 1:n 关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆