2014-10-29 3 views
0

내가 설정하는 간단한 블로그 애플리케이션을 시도하고를 사용하여 두 번 외래 키가 있습니까, 나는 다음과 같은 스키마 정의했다 : 여기왜 Sequelize

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 :

,132 10

표가 제대로 만들어되지만 난이 MySQL의에서 제 테이블입니다 예를 들어, 2 외래 키마다 끝낼 :

'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이 테이블의 필드를 만들었습니다.

답변

0

당신은 예컨대, 관계의 양쪽에 외부 키를 추가해야합니다

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