[Bug] HasMany relation key infering is broken, can't use HasMany with models with different key names
See original GitHub issueBug report
What I did
For a more detailed explanation I made this Stackoverflow post: https://stackoverflow.com/questions/72272377/column-not-found-1054-unknown-column-when-submitting-form-with-subfield-relatio
To be short, my issue happens when I attempt to implement this setup: https://backpackforlaravel.com/docs/5.x/crud-fields#manage-related-entries-in-the-same-form-create-update-delete with 2 models that override their primary key attribute (as their primary key is not “id”). Now unless there is something I REALLY don’t understand, i’m pretty sure that my relations are correctly defined.
I managed to reproduce the issue with the Backpack Demo project. Here is how you can you that:
-
Install demo project following instructions
-
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.
What I expected to happen
The form would submit without errors and my relations would be correctly inserted in database.
What happened
I get this error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id_story' in 'where clause'
.
What I’ve already tried to fix it
I triple checked my relations and read the corresponding Laravel documentation to make sure i’m not missing something. I followed the stack trace which lead me to this file: vendor\backpack\crud\src\app\Library\CrudPanel\Traits\Create.php:310
I ticked on this step because i don’t quite understand why the relations localKey
is used for this updateOrCreate
call, and i’m thinking the issue i’m having could come from this (although I tried playing around with it with no real success).
Is it a bug in the latest version of Backpack?
After I run composer update backpack/crud
the bug… is it still there?
Yes
Backpack, Laravel, PHP, DB version
When I run php artisan backpack:version
the output is:
PHP VERSION:
PHP 7.4.27 (cli) (built: Dec 14 2021 19:52:13) ( ZTS Visual C++ 2017 x64 )
Copyright © The PHP Group
Zend Engine v3.4.0, Copyright © Zend Technologies
with Xdebug v3.1.2, Copyright © 2002-2021, by Derick Rethans
LARAVEL VERSION:
v9.10.1@93414b1c7c0a56081d96c060bb850ac192d3d323
BACKPACK VERSION:
5.0.14@1e355c4c046a34423a1a3e3150120245a4bfd8e9
Issue Analytics
- State:
- Created a year ago
- Comments:9 (8 by maintainers)
Top GitHub Comments
Hello @singeryo I’ve been debugging this in the last few days, I needed to setup a testable scenario first so I could be sure if it was indeed a bug, or something you were missing in your definition. I can confirm we have an issue on how we get the related key in
HasMany
relation that would prevent people from using the functionality if they used different names for the keys.I’ve submitted https://github.com/Laravel-Backpack/CRUD/pull/4402 to address this issue, I’ve tested your specific scenario and everything is working fine after the PR.
It should be merged and tagged probably in the next monday.
Thanks again for the report and sticking with us finding the solution.
Wish you the best, Pedro
Merged! Will be available later today after I tag a new version. Thanks guys!