我有3个表,people
,instructors
和trainees
。讲师和学员继承人。三个表都与fee_instructor表有关。这是所有型号。
// Person.php
class Person extends \Eloquent {
// Add your validation rules here
public static $rules = [
// 'title' => 'required'
'name'=>'required',
'institution_id'=>'required',
'pob'=>'required',
'dob'=>'required'
];
// Don't forget to fill this array
protected $fillable = [
'name',
'title',
'position',
'institution_id',
'pob',
'dob',
'photo',
'last_education',
'nip',
'role_id'
];
public function fee(){
return $this->belongsToMany('Fee', 'fee_instructor', 'person_id');
}
}
// Instructor.php
class Instructor extends \Eloquent {
// Add your validation rules here
public static $rules = [
// 'title' => 'required'
];
// Don't forget to fill this array
protected $fillable = [
'name',
'title',
'position',
'institution_id',
'pob',
'dob',
'photo',
'email',
'last_education',
'nip'
];
public function fee(){
return $this->belongsToMany('Fee', 'fee_instructor', 'person_id');
}
}
// Trainee.php
class Trainee extends \Eloquent {
// Add your validation rules here
public static $rules = [
// 'title' => 'required'
];
// Don't forget to fill this array
protected $fillable = [
'name',
'title',
'position',
'institution_id',
'pob',
'dob',
'photo',
'last_education',
'nip',
'reg_date',
'company_name',
'marital_status',
'email'
];
public function fee(){
return $this->belongsToMany('Fee', 'fee_instructor', 'person_id');
}
}
// Fee.php
class Fee extends \Eloquent {
// Add your validation rules here
public static $rules = [
// 'title' => 'required'
];
// Don't forget to fill this array
protected $fillable = ['name', 'tarif', 'unit_id'];
public function unit(){
return $this->belongsTo('Unit');
}
public function instructor(){
return $this->belongsToMany('Instructor', 'fee_instructor');
}
public function person(){
return $this->belongsToMany('Person', 'fee_instructor');
}
public function trainee(){
return $this->belongsToMany('Trainee', 'fee_instructor');
}
}
当我尝试更新不属于讲师的条目时(在person
或trainee
中),laravel会抛出以下错误。
SQLSTATE[23503]: Foreign key violation: 7 ERROR: insert or update on table "fee_instructor" violates foreign key constraint "fee_instructor_instructor_id_foreign" DETAIL: Key (person_id)=(5) is not present in table "instructors".
在这些场景中使用数据透视表的正确方法是什么?
答案 0 :(得分:1)
啊所以你使用postgresql。那就是为什么我认为你谈论表继承是很奇怪的。请注意,根据文档http://www.postgresql.org/docs/9.0/static/ddl-inherit.html
Inheritance does not automatically propagate data from INSERT or COPY commands to other tables in the inheritance hierarchy.
INSERT always inserts into exactly the table specified.
...
In some cases it is possible to redirect the insertion using a rule (see Chapter 37).
因此,如果您不在规则的范围内,您可能会错误地认为instructor
中存在的行也存在于超级表person
中,反之亦然。您在fee_instructor
上的更新/插入失败只是因为person_id = 5
表中不存在instructor
的行。它可能存在于person
表中,但这是无关紧要的。如果instructor
中不存在,则无效。
如果您滚动浏览有关继承的文档,您还会提出使用继承的警告:
A serious limitation of the inheritance feature is that indexes (including unique constraints)
and foreign key constraints only apply to single tables, not to their inheritance children.
因此,如果您真的希望与fee_instructor
和instructor
的{{1}}建立FK关系,那么我假设您已经拥有的每个表都必须存在单独的FK。< / p>
请不要认为,雄辩的常规关系与继承的想法不相符。但是,您可以使用多态关系实现继承类型的行为:http://laravel.com/docs/4.2/eloquent#polymorphic-relations