【问题标题】:doctrine, symfony 1.4: How do I fix this 'Integrity constraint violation'学说,symfony 1.4:我该如何解决这个“违反完整性约束”
【发布时间】:2011-11-26 01:08:40
【问题描述】:

我已经在 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 无法删除或 更新父行:外键约束失败 (test2.sf_guard_user_profile, 约束 sf_guard_user_profile_user_id_sf_guard_user_id 外键 (user_id) 参考sf_guard_user (id) 删除级联)

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

是什么导致了错误?

编辑:如果我将seeker_id 中的primary 更改为false,它确实有效。但我希望外键成为主键。如果可能的话,我该怎么做?

【问题讨论】:

    标签: mysql orm symfony1 doctrine foreign-keys


    【解决方案1】:

    尝试将 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();
      }
    

    【讨论】:

      猜你喜欢
      • 2012-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-14
      • 1970-01-01
      • 2019-08-18
      • 2021-12-13
      相关资源
      最近更新 更多