baum icon indicating copy to clipboard operation
baum copied to clipboard

Issue with custom primary keys

Open SuperSeb92 opened this issue 9 years ago • 3 comments

When using a custom primary key in a model

protected $primaryKey = 'reference';

and the following migration

        $table->string('reference', 20)->primary();
        $table->string('parent_id', 20)->nullable();
        $table->integer('lft')->nullable();
        $table->integer('rgt')->nullable();
        $table->integer('depth')->nullable();

The following:

    $things = [
          ['reference' => 'One'],
          [
               'reference' => 'Two',
               'children' => [
                    ['reference' => 'TwoDotOne'],
                    ['reference' => 'TwoDotTwo']
                 ]
          ],
          ['reference' => 'Three']
     ];
     App\Thing::buildTree($things);

results in a "No query results for model" Error, triggered by the $self->reload() in Node@setDepth.

I guess this is because we're reloading before the new model is persisted?

SuperSeb92 avatar Jan 06 '16 01:01 SuperSeb92

You're right about the call to reload, but I'm not sure why this only affects models with a custom primary key.

Could you write a failing unit test + migration to demonstrate the issue please? If you fork the repo and make a pull request, I can then take a look. Thanks.

gazsp avatar Jan 06 '16 21:01 gazsp

I'm having similar problem with similar migration.

When I want to save a model that value of custom primary key is float number, then results in a "No query results for model" Error, triggered by the $self->reload() in Node@setDepth, but model is saved in database.

When the value of custom primary key has at least one non-numeric character, then it works well.

ArbnorZeqiri avatar Mar 14 '16 09:03 ArbnorZeqiri

Hey, I found the solution.

try setting $incrementing = false in your model.

That worked for me.

ArbnorZeqiri avatar Mar 23 '16 12:03 ArbnorZeqiri