doctrine,symfony 1.4:如何解决这个'完整性约束违规'

时间:2011-09-27 13:08:44

标签: mysql orm symfony1 doctrine foreign-keys

我已经在schema.yml中定义了这个实体

Jobsearch:
  tableName: jobsearch
  columns:
    seeker_id:
      primary: true
      type: integer
      notnull: true
      autoincrement: false
    empmode:
      type: string(50)
    pensum:
      type: integer
    active:
      type: boolean
  relations:
    Privateaccount:
      local: seeker_id
      foreign: id
      alias: seeker

它有一个

的外键引用
Privateaccount:
  tableName: privateaccount
  columns:
    id:
      primary: true
      unique: true
      type: integer
      notnull: true
      autoincrement: true
    firstname:
      type: string(255)
    lastname:
      type: string(255)
  inheritance:
    extends: sfGuardUser
    type: column_aggregation
    keyField: type
    keyValue: Privateaccount

我为测试目的做了一个symfony动作,它应该将Jobsearch保存到db:

 public function executeTest(sfWebRequest $request)
  {
   $test = new Jobsearch();
   $test->setSeeker( $this->getUser()->getGuardUser() ) ; // set the user that is logged in    
   $test->save();
  }

$test->save()会导致此错误:

  

SQLSTATE [23000]:完整性约束违规:1451无法删除或   更新父行:外键约束失败   (test2sf_guard_user_profile,CONSTRAINT   sf_guard_user_profile_user_id_sf_guard_user_id外国钥匙   (user_id)参考sf_guard_userid)ON DELETE CASCADE)

我不明白为什么外键约束失败。

导致错误的原因是什么?

编辑:如果我在primary中将false更改为seeker_id,则确实有效。但我希望外键成为主键。如果可能的话,我该如何做到这一点?

1 个答案:

答案 0 :(得分:0)

尝试将schema.yml更改为

Jobsearch:
  tableName: jobsearch
  columns:
    seeker_id:
      primary: true
      type: integer
      notnull: true
      autoincrement: false
    empmode:
      type: string(50)
    pensum:
      type: integer
    active:
      type: boolean
  relations:
    Seeker:
      class: Privateaccount
      local: seeker_id

Privateaccount:
  tableName: privateaccount
  columns:
    firstname:
      type: string(255)
    lastname:
      type: string(255)
  inheritance:
    extends: sfGuardUser
    type: column_aggregation
    keyField: type
    keyValue: Privateaccount

Doctrine默认创建id字段。不要声明它。

最后,它应该有用。

 public function executeTest(sfWebRequest $request)
  {
   $test = new Jobsearch();
   $test->setSeekerId( $this->getUser()->getGuardUser()->getId() ) ; // set the user that is logged in    
   $test->save();
  }