question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug] HasMany relation key infering is broken, can't use HasMany with models with different key names

See original GitHub issue

Bug 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:closed
  • Created a year ago
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
pxpmcommented, May 26, 2022

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

0reactions
tabacitucommented, May 30, 2022

Merged! Will be available later today after I tag a new version. Thanks guys!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Laravel - Eloquent relationship (hasMany) issues
The reason the ID names are not just 'id' is because i am importing the order data from a external API. I think...
Read more >
Relationships in Analysis Services tabular models
In tabular models, a relationship is a connection between two tables of data. ... In a relational database, there are several types of...
Read more >
E-R Model Case Studies 1 - JIS College of Engineering
Answer a : b) Construct an alternative E-R diagram that uses only a binary relationship between students and course-offerings. Make sure that only...
Read more >
Eloquent Relations - getting parent data from child not working
namespace App\Models; use Illuminate\Database\Eloquent\Model; ... bug in Laravel's hasMany / belongsTo when a non-standard Laravel primary key name is used.
Read more >
NVIDIA Deep Learning TensorRT Documentation
Abstract. This NVIDIA TensorRT Developer Guide demonstrates how to use the C++ and Python APIs for implementing the most common deep learning layers....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found