2017-03-17 22 views
1

3つの外部キーと個人属性を持たないリレーションシップテーブルを作成する最適な方法は何ですか。 私はsubjectが含まれている関係テーブルteacher_subject_allocationを作成したい、teachersection3つの外部キーとプライベート属性を持つテーブルを後継に作成

subject.belongsToMany(teacher, {through: 'teacher_subject_allocation'})

teacher.belongsToMany(section, {through: 'teacher_subject_allocation'}) 
+0

あなたはなし個人属性とはどういう意味ですか? – Shaharyar

+0

自身の属性はありません –

答えて

0
//main tables 
//(foreignKey contains field name in association table) 
subject.hasOne(teacher_subject_allocation , { foreignKey: 'subject_id' }); 
teacher.hasOne(teacher_subject_allocation , { foreignKey: 'teacher_id' }); 
section.hasOne(teacher_subject_allocation , { foreignKey: 'section_id' }); 

//association table 
//(targetKey is PK of main table, foreignKey is field of current table) 
teacher_subject_allocation.belongsTo(subject, { targetKey: 'id', foreignKey: 'subject_id' }); 
teacher_subject_allocation.belongsTo(teacher, { targetKey: 'id', foreignKey: 'teacher_id' }); 
teacher_subject_allocation.belongsTo(section, { targetKey: 'id', foreignKey: 'section_id' }); 
+0

ですが、 "teacher_subject_allocation"は存在しません。 belongsToManyは自動的に関係テーブルを作成します。 –

+0

自分で作成した場合は作成しません。これら3つの列で 'teacher_subject_allocation'を作成し、モデルを作成したくない場合は、 – Shaharyar

+0

の関連付けを適用します。その他の方法で ? –

関連する問題