【问题标题】:Why do I have twice the foreign keys using Sequelize为什么我使用 Sequelize 有两倍的外键
【发布时间】:2014-10-29 15:39:02
【问题描述】:

我正在尝试设置一个简单的博客应用程序,我有以下架构定义:

这里是User

module.exports = function(sequelize, DataTypes) {
    var User = sequelize.define("User", {
        id: {type: DataTypes.BIGINT, autoincrement:true, primaryKey: true},
        firstName: {type: DataTypes.STRING},
        lastName: {type: DataTypes.STRING},
        nickName: {type: DataTypes.STRING},
        email: {type: DataTypes.STRING, unique: true, comment: "Unique "},
        password: {type: DataTypes.STRING},
        salt: {type: DataTypes.STRING},
        enabled: {type: DataTypes.BOOLEAN, allowNull: false, defaultValue: true}
    }, {
        freezeTableName: true,
        classMethods: {
            associate: function (models) {
                User.hasMany(models.Article, {as: "Articles", constraints: false});
                User.hasMany(models.Comment, {as: "Comments", constraints: false});
            }
        }
    });

    return User;
};

这里是Article

module.exports = function(sequelize, DataTypes) {
    var Article = sequelize.define("Article", {
        id: {type: DataTypes.BIGINT, autoincrement:true, primaryKey: true},
        slug: {type: DataTypes.STRING, comment: "Unique URL slug to access the article"},
        title: {type: DataTypes.STRING},
        content: {type: DataTypes.TEXT},
        created: {type: DataTypes.DATE, defaultValue: DataTypes.NOW},
        published: {type: DataTypes.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", constraints: false});
            }
        }
    });

    return Article;
};

Comment:

module.exports = function(sequelize, DataTypes) {
    var Comment = sequelize.define("Comment", {
        id: {type: DataTypes.BIGINT, autoincrement:true, primaryKey: true},
        content: {type: DataTypes.TEXT},
        status: {type: DataTypes.INTEGER, defaultValue: 1},
        author: {type: DataTypes.BIGINT},
        article: {type: DataTypes.BIGINT}
    }, {
        freezeTableName: true,
        classMethods: {
            associate: function (models) {
                Comment.hasOne(Comment, {as : "Parent", foreignKey: "parent_id"});
                Comment.belongsTo(models.User, {as: "Author", foreignKey: "author_id"});
                Comment.belongsTo(models.Article, {as: "Article", foreignKey: "article_id"});
            }
        }
    });

    return Comment;
};

这些表是正确创建的,但我每次都有 2 个外键,例如这是 MySQL 中的 Article 表:

'id','bigint(20)','NO','PRI','0',''
'slug','varchar(255)','YES','',NULL,''
'title','varchar(255)','YES','',NULL,''
'content','text','YES','',NULL,''
'created','datetime','YES','',NULL,''
'published','tinyint(1)','NO','','1',''
'createdAt','datetime','NO','',NULL,''
'updatedAt','datetime','NO','',NULL,''
'author_id','bigint(20)','YES','MUL',NULL,''
'UserId','bigint(20)','YES','',NULL,''

UserId == author_id

用户表:

'id','bigint(20)','NO','PRI','0',''
'firstName','varchar(255)','YES','',NULL,''
'lastName','varchar(255)','YES','',NULL,''
'nickName','varchar(255)','YES','',NULL,''
'email','varchar(255)','YES','UNI',NULL,''
'password','varchar(255)','YES','',NULL,''
'salt','varchar(255)','YES','',NULL,''
'enabled','tinyint(1)','NO','','1',''
'createdAt','datetime','NO','',NULL,''
'updatedAt','datetime','NO','',NULL,''

这个表是正确的(没有外键)

评论:

'id','bigint(20)','NO','PRI','0',''
'content','text','YES','',NULL,''
'status','int(11)','YES','','1',''
'author','bigint(20)','YES','',NULL,''
'article','bigint(20)','YES','',NULL,''
'createdAt','datetime','NO','',NULL,''
'updatedAt','datetime','NO','',NULL,''
'ArticleId','bigint(20)','YES','',NULL,''
'parent_id','bigint(20)','YES','MUL',NULL,''
'author_id','bigint(20)','YES','MUL',NULL,''
'article_id','bigint(20)','YES','MUL',NULL,''
'UserId','bigint(20)','YES','',NULL,''

ArticleId == article_idUserId == author_id

如您所见,我有骆驼装的版本和我指定的版本。我错过了什么?

** 编辑 **

数据库中没有骆驼案例字段的限制:UserIdArticleId 但 Sequelize 在表中创建了字段。

【问题讨论】:

    标签: node.js associations sequelize.js


    【解决方案1】:

    需要在关系的两边加上外键,例如:

    User.hasMany(models.Article, {constraints: false, foreignKey: 'author_id'});
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 2020-11-26
      • 1970-01-01
      • 2022-08-18
      • 1970-01-01
      • 2012-10-01
      相关资源
      最近更新 更多