Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overriding $primaryKey is still an issue #1958

Closed
paouriel opened this issue Feb 11, 2020 · 9 comments
Closed

Overriding $primaryKey is still an issue #1958

paouriel opened this issue Feb 11, 2020 · 9 comments
Labels

Comments

@paouriel
Copy link

paouriel commented Feb 11, 2020

  • Laravel-mongodb Version: 3.6.1
  • PHP Version: 7.3.14
  • Database Driver & Version: mongodb / mongo v4.2.3

Description:

I am trying to use a custom primary key, but it persists on using _id. After checking similar issues, I found out it posted here #1354 and fixed here #1392.

I checked the code from the library and saw it wasn't changed.
Here is the link to the file from master branch:
https://github.com/jenssegers/laravel-mongodb/blob/master/src/Jenssegers/Mongodb/Query/Builder.php#L189)

Here is the link to the file from merge request:
https://github.com/jenssegers/laravel-mongodb/pull/1392/files

Am I missing something here?

Steps to reproduce

  1. Add the following code to your model:
    protected $primaryKey = 'fieldName'

  2. Run php artisan migrate:refresh

Expected behaviour

I should be able to use a custom primary key.

Actual behaviour

The model uses a hard-coded string _id for its primary key

Logs:

[2020-02-11 01:41:07] local.ERROR: Undefined index: rid {"exception":"[object] (ErrorException(code: 0): Undefined index: rid at /var/www/vendor/jenssegers/mongodb/src/Jenssegers/Mongodb/Query/Builder.php:582)
[stacktrace]
#0 /var/www/vendor/jenssegers/mongodb/src/Jenssegers/Mongodb/Query/Builder.php(582): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(8, 'Undefined index...', '/var/www/vendor...', 582, Array)
#1 /var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(1366): Jenssegers\Mongodb\Query\Builder->insertGetId(Array, 'rid')
#2 /var/www/vendor/jenssegers/mongodb/src/Jenssegers/Mongodb/Eloquent/Builder.php(78): Illuminate\Database\Eloquent\Builder->__call('insertGetId', Array)
#3 /var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(839): Jenssegers\Mongodb\Eloquent\Builder->insertGetId(Array, 'rid')
#4 /var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(804): Illuminate\Database\Eloquent\Model->insertAndSetId(Object(Jenssegers\Mongodb\Eloquent\Builder), Array)
#5 /var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(667): Illuminate\Database\Eloquent\Model->performInsert(Object(Jenssegers\Mongodb\Eloquent\Builder))
#6 /var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php(219): Illuminate\Database\Eloquent\Model->save()
#7 /var/www/vendor/laravel/framework/src/Illuminate/Support/Traits/EnumeratesValues.php(176): Illuminate\Database\Eloquent\FactoryBuilder->Illuminate\Database\Eloquent\{closure}(Object(App\Student), 0)
#8 /var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php(220): Illuminate\Support\Collection->each(Object(Closure))
#9 /var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php(181): Illuminate\Database\Eloquent\FactoryBuilder->store(Object(Illuminate\Support\Collection))
#10 /var/www/database/seeds/StudentTeacherSeeder.php(19): Illuminate\Database\Eloquent\FactoryBuilder->create(Array)
#11 /var/www/vendor/laravel/framework/src/Illuminate/Support/Traits/EnumeratesValues.php(176): StudentTeacherSeeder->{closure}(Object(App\Teacher), 0)
#12 /var/www/database/seeds/StudentTeacherSeeder.php(27): Illuminate\Support\Collection->each(Object(Closure))
#13 [internal function]: StudentTeacherSeeder->run()
#14 /var/www/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(32): call_user_func_array(Array, Array)
#15 /var/www/vendor/laravel/framework/src/Illuminate/Container/Util.php(36): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#16 /var/www/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(90): Illuminate\Container\Util::unwrapIfClosure(Object(Closure))
#17 /var/www/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(34): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))
#18 /var/www/vendor/laravel/framework/src/Illuminate/Container/Container.php(590): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)
#19 /var/www/vendor/laravel/framework/src/Illuminate/Database/Seeder.php(134): Illuminate\Container\Container->call(Array)
#20 /var/www/vendor/laravel/framework/src/Illuminate/Database/Seeder.php(48): Illuminate\Database\Seeder->__invoke()
#21 /var/www/database/seeds/DatabaseSeeder.php(17): Illuminate\Database\Seeder->call('StudentTeacherS...')
#22 [internal function]: DatabaseSeeder->run()
#23 /var/www/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(32): call_user_func_array(Array, Array)
#24 /var/www/vendor/laravel/framework/src/Illuminate/Container/Util.php(36): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#25 /var/www/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(90): Illuminate\Container\Util::unwrapIfClosure(Object(Closure))
#26 /var/www/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(34): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))
#27 /var/www/vendor/laravel/framework/src/Illuminate/Container/Container.php(590): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)
#28 /var/www/vendor/laravel/framework/src/Illuminate/Database/Seeder.php(134): Illuminate\Container\Container->call(Array)
#29 /var/www/vendor/laravel/framework/src/Illuminate/Database/Console/Seeds/SeedCommand.php(63): Illuminate\Database\Seeder->__invoke()
#30 /var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/GuardsAttributes.php(122): Illuminate\Database\Console\Seeds\SeedCommand->Illuminate\Database\Console\Seeds\{closure}()
#31 /var/www/vendor/laravel/framework/src/Illuminate/Database/Console/Seeds/SeedCommand.php(64): Illuminate\Database\Eloquent\Model::unguarded(Object(Closure))
#32 [internal function]: Illuminate\Database\Console\Seeds\SeedCommand->handle()
#33 /var/www/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(32): call_user_func_array(Array, Array)
#34 /var/www/vendor/laravel/framework/src/Illuminate/Container/Util.php(36): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#35 /var/www/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(90): Illuminate\Container\Util::unwrapIfClosure(Object(Closure))
#36 /var/www/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(34): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))
#37 /var/www/vendor/laravel/framework/src/Illuminate/Container/Container.php(590): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)
#38 /var/www/vendor/laravel/framework/src/Illuminate/Console/Command.php(201): Illuminate\Container\Container->call(Array)
#39 /var/www/vendor/symfony/console/Command/Command.php(255): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArrayInput), Object(Illuminate\Console\OutputStyle))
#40 /var/www/vendor/laravel/framework/src/Illuminate/Console/Command.php(188): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArrayInput), Object(Illuminate\Console\OutputStyle))
#41 /var/www/vendor/laravel/framework/src/Illuminate/Console/Concerns/CallsCommands.php(56): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArrayInput), Object(Illuminate\Console\OutputStyle))
#42 /var/www/vendor/laravel/framework/src/Illuminate/Console/Concerns/CallsCommands.php(28): Illuminate\Console\Command->runCommand('db:seed', Array, Object(Illuminate\Console\OutputStyle))
#43 /var/www/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/FreshCommand.php(79): Illuminate\Console\Command->call('db:seed', Array)
#44 /var/www/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/FreshCommand.php(56): Illuminate\Database\Console\Migrations\FreshCommand->runSeeder(NULL)
#45 [internal function]: Illuminate\Database\Console\Migrations\FreshCommand->handle()
#46 /var/www/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(32): call_user_func_array(Array, Array)
#47 /var/www/vendor/laravel/framework/src/Illuminate/Container/Util.php(36): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#48 /var/www/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(90): Illuminate\Container\Util::unwrapIfClosure(Object(Closure))
#49 /var/www/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(34): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))
#50 /var/www/vendor/laravel/framework/src/Illuminate/Container/Container.php(590): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)
#51 /var/www/vendor/laravel/framework/src/Illuminate/Console/Command.php(201): Illuminate\Container\Container->call(Array)
#52 /var/www/vendor/symfony/console/Command/Command.php(255): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
#53 /var/www/vendor/laravel/framework/src/Illuminate/Console/Command.php(188): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
#54 /var/www/vendor/symfony/console/Application.php(1012): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#55 /var/www/vendor/symfony/console/Application.php(272): Symfony\Component\Console\Application->doRunCommand(Object(Illuminate\Database\Console\Migrations\FreshCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#56 /var/www/vendor/symfony/console/Application.php(148): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#57 /var/www/vendor/laravel/framework/src/Illuminate/Console/Application.php(93): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#58 /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(131): Illuminate\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#59 /var/www/artisan(37): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#60 {main}
"}

@divine
Copy link
Contributor

divine commented Feb 11, 2020

Hello,

That's definitely a bug, however not sure why it was reverted back to fix a "tests"?

No explanation was given from @jenssegers so we're on just our own.

db98372#r26758163

@Smolevich @Giacomo92 @rennokki any suggestions what we need to do?

Thank you!

@Giacomo92
Copy link

php artisan migrate:refresh is unuseful because MongoDB is schema less. So if you run that command the structure will not be updated.

You should create a command to do that

@andycansdale
Copy link

@paouriel I experienced this same issue today and I tracked it down to having tried to create a record with a null value for my custom primary key. When passing a valid value it to works fine.

Perhaps an error message for when the custom primary key is null would be helpful.

@Smolevich Smolevich added the Needs investigation Need investigation about bugs described in issue label Feb 25, 2020
@DaniloBenevides
Copy link

Is there any update?
I have checked the code and the correction still not applied, "_id" is hardcoded on Jessengers/Mongodb/Query/Builder on version 3.6.4

@stephenjude
Copy link
Contributor

@Smolevich I have looked into this issue and found that this issue exists. The Builder uses a hard-coded string _id for its primary key. Should I make a pull request to fix this?

@divine
Copy link
Contributor

divine commented Jul 18, 2020

@stephandesouza it's an easy fix but some tests are failing. This is the main reason why it's being delayed.

However, I might take a look soon.

@divine
Copy link
Contributor

divine commented Sep 16, 2020

I've just checked this and can confirm that there is no issue at all.

See this test and this model

$check = Book::find('A Game of Thrones');
$this->assertEquals('title', $check->getKeyName());
$this->assertEquals('A Game of Thrones', $check->getKey());
$this->assertEquals('A Game of Thrones', $check->title);

It looks like hard coded, however it's being changed when queried within Eloquent, see this function which is being used in this library.

If it doesn't work for you, then check keyType is defined correctly (by default it's being converted to string)

Thanks!

@divine divine closed this as completed Sep 16, 2020
@divine divine added invalid and removed Needs investigation Need investigation about bugs described in issue labels Sep 16, 2020
@stephenjude
Copy link
Contributor

Okay

@erickneverson
Copy link

For me, it was definitely about specifying the keyType as @divine stated. I'll leave a snippet from my model here in case anyone is wondering:

protected $primaryKey = 'user_id';
protected $keyType = 'int';

@mongodb mongodb locked as resolved and limited conversation to collaborators May 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

8 participants