我有 3 个表,人员
、讲师
和受训人员
。讲师和受训者继承自人。这三个表都与 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".
在这些情况下如何正确使用数据透视表?
最佳答案
啊,所以你使用的是 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
中存在的行也存在于 super 表 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.
因此,如果您真的想为 instructor
和 trainee
与 fee_instructor
建立 FK 关系,那么每个表都必须存在单独的 FK,我'我假设您已经准备就绪。
也请注意,eloquent 的正则关系与继承的思想不相符。但是,您可以使用多态关系实现继承类型的行为:http://laravel.com/docs/4.2/eloquent#polymorphic-relations
关于php - 继承表的共享数据透视表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27073682/