学说,symfony 1.4:如何解决此“完整性约束违规”问题?

发布于 2024-12-06 15:02:06 字数 1614 浏览 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:
    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 保存到数据库:

 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) ON DELETE CASCADE)

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

是什么导致了这个错误?

编辑:如果我将 seeker_id 中的 primary 更改为 false,它确实有效。但我希望外键成为主键。如果可能的话,我该如何使其发挥作用?

I have defined this entity in 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

it has a foreign key reference to

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

I made a symfony action for testing purpose, it is supposed to save a Jobsearch to 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() results in this error:

SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or
update a parent row: a foreign key constraint fails
(test2.sf_guard_user_profile, CONSTRAINT
sf_guard_user_profile_user_id_sf_guard_user_id FOREIGN KEY
(user_id) REFERENCES sf_guard_user (id) ON DELETE CASCADE)

I don't see why the foreign key constraint is failing.

What has caused the error?

EDIT: If I change primary to false in seeker_id, it does work. But I want the Foreign Key to be the Primary Key. If possible, how do I make that work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只等公子 2024-12-13 15:02:06

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

Try changing the schema.yml to

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

and

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

Doctrine creates the id field by default. Don't declare it.

Finally, it should work.

 public function executeTest(sfWebRequest $request)
  {
   $test = new Jobsearch();
   $test->setSeekerId( $this->getUser()->getGuardUser()->getId() ) ; // set the user that is logged in    
   $test->save();
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文