找不到列:1054未知列与子场关系提交表格

发布于 2025-01-29 10:52:19 字数 2122 浏览 0 评论 0原文

这个问题在背包V5和Laravel 8上发生在我身上(尽管我也在Laravel 9上也重现了它)。

当我拥有类似于此设置的设置时,我的问题就会发生: https://backpackforlaravel.com/docs/5.x/crud-fields#manage-related-entrated-entries-in-the-the-the-same-form-createe-create--create--create-portate-update-delete

在这里澄清:

  • 两个模型与1-N关系相关的
  • 这些模型这些模型覆盖其$ priendary属性(作为其主要密钥不是“ id”),
  • “父级”模型crud crud crud具有'type'=> 与'子场'

“关系

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id_story' in 'where clause' 

select * from `monsters` where `monsters`.`story_id` = 5 and `monsters`.`story_id` is not null and (`id_story` is null) limit 1

” 代码>供应商\ backpack \ crud \ src \ app \ app \ library \ crudpanel \ traits \ traits \ create.php:310

以下是我在背包demo projet上重现它的

  • 方式a href =“ https://backpackforlaravel.com/docs/4.1/demo#demo-installation” rel =“ nofollow noreferrer”> https://backpackforlaravel.com/docs/docs/docs/ddocs/4.1/demo#demo-unstallation

  • 重命名怪物表中的主键id_monster and stories id_story主要键< /代码>

  • 添加的主要键$ priendarkey在相应的模型上覆盖:

      preected $ priendarkey ='id_monster'; //在monster.php中
     受保护的$ priendarkey ='id_story'; // in Story.php
     
  • 添加正确的“ foreferkey”参数的相应模型中的关系:

    so:

      // Story.php中
      公共功能怪物()
      {
         返回$ this-&gt; hasmany(\ app \ models \ monster :: class,'story_id');
      }
    
      //在monster.php中
      公共功能故事()
      {
         返回$ this-&gt;
      }
     

现在通过CRUD面板添加一个带有怪物的故事,您应该重现错误。

我认为我可能缺少一些东西,但我无法弄清楚。我很确定关系已正确定义,我想知道这是否不是背包中的错误。

感谢您的帮助 !

更新

这确实是一个已报告的问题在这里

This issue happens for me on Backpack v5 and Laravel 8 (although I do reproduce it on Laravel 9 as well).

My issue happens when I have a setup similar to this one : https://backpackforlaravel.com/docs/5.x/crud-fields#manage-related-entries-in-the-same-form-create-update-delete

Just to clarify here:

  • Two models related by a 1-n relationship
  • Those models override their $primaryKey attribute (as their primary key is not "id")
  • the "parent" models CRUD that has a 'type' => 'relationship' with 'subfields' to add children through a "repeatable" type field

This is the error I get (with the Monsters and Stories example):

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id_story' in 'where clause' 

select * from `monsters` where `monsters`.`story_id` = 5 and `monsters`.`story_id` is not null and (`id_story` is null) limit 1

The stack trace leads me to vendor\backpack\crud\src\app\Library\CrudPanel\Traits\Create.php:310

Here is how I reproduced it on the Backpack demo projet:

  • Install demo project following instructions here :https://backpackforlaravel.com/docs/4.1/demo#demo-installation

  • Rename monsters primary key in table to id_monster and stories primary key to id_story

  • Add the $primaryKey overrides on corresponding models like so:

     protected $primaryKey = 'id_monster'; // In Monster.php
     protected $primaryKey = 'id_story'; // In Story.php
    
  • Add correct "foreignKey" parameters to relation method like so:

      // In Story.php
      public function monsters()
      {
         return $this->hasMany(\App\Models\Monster::class, 'story_id');
      }
    
      // In Monster.php
      public function story()
      {
         return $this->belongsTo(\App\Models\Story::class, 'story_id');
      }
    

Now add a story with a monster through the Crud panel and you should reproduce the error.

I'm thinking I'm probably missing something but I just can't figure it out. I'm pretty sure the relations are properly defined and I'm wondering if it's not a bug in Backpack.

Thanks for your help !

UPDATE

This is indeed an issue that has been reported here

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

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

发布评论

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

评论(1

夜清冷一曲。 2025-02-05 10:52:19

我看到您在两个关系中都使用“ story_id”(story.php和monster.php)。

在Story.php中,您应该使用:

  public function monsters()
  {
     return $this->hasMany(\App\Models\Monster::class, 'story_id');
  }

在Monster.php中,您应该使用:

  public function story()
  {
     return $this->belongsTo(\App\Models\Story::class, 'monster_id');
  }

还要注意,当您尝试在迁移和模型中设置关系时,您将在迁移和模型中设置正确的外键。

I'm seeing that you are using "story_id" in both relations (Story.php and Monster.php).

In Story.php you should use :

  public function monsters()
  {
     return $this->hasMany(\App\Models\Monster::class, 'story_id');
  }

And In Monster.php you should use :

  public function story()
  {
     return $this->belongsTo(\App\Models\Story::class, 'monster_id');
  }

Also be careful that when you are trying to set relationships in migrations and in models, you are setting correct foreign keys both in migration and models.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文