在 heroku 中播种枢轴 table 会导致错误

Seeding a pivot table in heroku results to error

我正在 Heroku 中部署一个带有数据库的 Laravel 5.2 应用程序。在为 table 播种之前,我已经成功迁移了我的 table 且没有错误。播种总是导致这样的错误:

Seeded: RolesTableSeeder
Seeded: PrivilegesTableSeeder
[2021-07-19 18:40:57] production.ERROR: PDOException: SQLSTATE[42703]: Undefined column: 7 ERROR:  column "id" does not exist
LINE 1: ..." ("role_id", "privilege_id") values (, ) returning "id"
                                                                   ^ in /app/vendor/laravel/framework/src/Illuminate/Database/Connection.php:335
Stack trace:
#0 /app/vendor/laravel/framework/src/Illuminate/Database/Connection.php(335): PDOStatement->execute()
#1 /app/vendor/laravel/framework/src/Illuminate/Database/Connection.php(722): Illuminate\Database\Connection->Illuminate\Database\{closure}()
#2 /app/vendor/laravel/framework/src/Illuminate/Database/Connection.php(685): Illuminate\Database\Connection->runQueryCallback()
#3 /app/vendor/laravel/framework/src/Illuminate/Database/Connection.php(349): Illuminate\Database\Connection->run()
#4 /app/vendor/laravel/framework/src/Illuminate/Database/Connection.php(312): Illuminate\Database\Connection->select()
#5 /app/vendor/laravel/framework/src/Illuminate/Database/Query/Processors/PostgresProcessor.php(20): Illuminate\Database\Connection->selectFromWriteConnection()
#6 /app/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(2107): Illuminate\Database\Query\Processors\PostgresProcessor->processInsertGetId()
#7 [internal function]: Illuminate\Database\Query\Builder->insertGetId()
#8 /app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(1423): call_user_func_array()
#9 /app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1628): Illuminate\Database\Eloquent\Builder->__call()
#10 /app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1597): Illuminate\Database\Eloquent\Model->insertAndSetId()
#11 /app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1488): Illuminate\Database\Eloquent\Model->performInsert()
#12 /app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(574): Illuminate\Database\Eloquent\Model->save()
#13 /app/database/seeds/RolePrivilegesTableSeeder.php(33): Illuminate\Database\Eloquent\Model::create()
#14 /app/database/seeds/RolePrivilegesTableSeeder.php(17): RolePrivilegesTableSeeder->addPrivilege()
#15 /app/vendor/laravel/framework/src/Illuminate/Database/Seeder.php(39): RolePrivilegesTableSeeder->run()
#16 /app/database/seeds/DatabaseSeeder.php(17): Illuminate\Database\Seeder->call()
#17 /app/vendor/laravel/framework/src/Illuminate/Database/Console/Seeds/SeedCommand.php(63): DatabaseSeeder->run()
#18 /app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(2314): Illuminate\Database\Console\Seeds\SeedCommand->Illuminate\Database\Console\Seeds\{closure}()
#19 /app/vendor/laravel/framework/src/Illuminate/Database/Console/Seeds/SeedCommand.php(64): Illuminate\Database\Eloquent\Model::unguarded()
#20 [internal function]: Illuminate\Database\Console\Seeds\SeedCommand->fire()
#21 /app/vendor/laravel/framework/src/Illuminate/Container/Container.php(507): call_user_func_array()
#22 /app/vendor/laravel/framework/src/Illuminate/Console/Command.php(169): Illuminate\Container\Container->call()
#23 /app/vendor/symfony/console/Command/Command.php(256): Illuminate\Console\Command->execute()
#24 /app/vendor/laravel/framework/src/Illuminate/Console/Command.php(155): Symfony\Component\Console\Command\Command->run()
#25 /app/vendor/symfony/console/Application.php(794): Illuminate\Console\Command->run()
#26 /app/vendor/symfony/console/Application.php(186): Symfony\Component\Console\Application->doRunCommand()
#27 /app/vendor/symfony/console/Application.php(117): Symfony\Component\Console\Application->doRun()
#28 /app/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(107): Symfony\Component\Console\Application->run()
#29 /app/artisan(35): Illuminate\Foundation\Console\Kernel->handle()
#30 {main}

Next Illuminate\Database\QueryException: SQLSTATE[42703]: Undefined column: 7 ERROR:  column "id" does not exist
LINE 1: ..." ("role_id", "privilege_id") values (, ) returning "id"
                                                                   ^ (SQL: insert into "role_privileges" ("role_id", "privilege_id") values (1, 1) returning "id") in /app/vendor/laravel/framework/src/Illuminate/Database/Connection.php:729
Stack trace:
#0 /app/vendor/laravel/framework/src/Illuminate/Database/Connection.php(685): Illuminate\Database\Connection->runQueryCallback()
#1 /app/vendor/laravel/framework/src/Illuminate/Database/Connection.php(349): Illuminate\Database\Connection->run()
#2 /app/vendor/laravel/framework/src/Illuminate/Database/Connection.php(312): Illuminate\Database\Connection->select()
#3 /app/vendor/laravel/framework/src/Illuminate/Database/Query/Processors/PostgresProcessor.php(20): Illuminate\Database\Connection->selectFromWriteConnection()
#4 /app/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(2107): Illuminate\Database\Query\Processors\PostgresProcessor->processInsertGetId()
#5 [internal function]: Illuminate\Database\Query\Builder->insertGetId()
#6 /app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(1423): call_user_func_array()
#7 /app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1628): Illuminate\Database\Eloquent\Builder->__call()
#8 /app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1597): Illuminate\Database\Eloquent\Model->insertAndSetId()
#9 /app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1488): Illuminate\Database\Eloquent\Model->performInsert()
#10 /app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(574): Illuminate\Database\Eloquent\Model->save()
#11 /app/database/seeds/RolePrivilegesTableSeeder.php(33): Illuminate\Database\Eloquent\Model::create()
#12 /app/database/seeds/RolePrivilegesTableSeeder.php(17): RolePrivilegesTableSeeder->addPrivilege()
#13 /app/vendor/laravel/framework/src/Illuminate/Database/Seeder.php(39): RolePrivilegesTableSeeder->run()
#14 /app/database/seeds/DatabaseSeeder.php(17): Illuminate\Database\Seeder->call()
#15 /app/vendor/laravel/framework/src/Illuminate/Database/Console/Seeds/SeedCommand.php(63): DatabaseSeeder->run()
#16 /app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(2314): Illuminate\Database\Console\Seeds\SeedCommand->Illuminate\Database\Console\Seeds\{closure}()
#17 /app/vendor/laravel/framework/src/Illuminate/Database/Console/Seeds/SeedCommand.php(64): Illuminate\Database\Eloquent\Model::unguarded()
#18 [internal function]: Illuminate\Database\Console\Seeds\SeedCommand->fire()
#19 /app/vendor/laravel/framework/src/Illuminate/Container/Container.php(507): call_user_func_array()
#20 /app/vendor/laravel/framework/src/Illuminate/Console/Command.php(169): Illuminate\Container\Container->call()
#21 /app/vendor/symfony/console/Command/Command.php(256): Illuminate\Console\Command->execute()
#22 /app/vendor/laravel/framework/src/Illuminate/Console/Command.php(155): Symfony\Component\Console\Command\Command->run()
#23 /app/vendor/symfony/console/Application.php(794): Illuminate\Console\Command->run()
#24 /app/vendor/symfony/console/Application.php(186): Symfony\Component\Console\Application->doRunCommand()
#25 /app/vendor/symfony/console/Application.php(117): Symfony\Component\Console\Application->doRun()
#26 /app/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(107): Symfony\Component\Console\Application->run()
#27 /app/artisan(35): Illuminate\Foundation\Console\Kernel->handle()
#28 {main}



  [Illuminate\Database\QueryException]
  SQLSTATE[42703]: Undefined column: 7 ERROR:  column "id" does not exist
  LINE 1: ..." ("role_id", "privilege_id") values (, ) returning "id"
                                                                     ^ (SQL:
  insert into "role_privileges" ("role_id", "privilege_id") values (1, 1) ret
  urning "id")



  [PDOException]
  SQLSTATE[42703]: Undefined column: 7 ERROR:  column "id" does not exist
  LINE 1: ..." ("role_id", "privilege_id") values (, ) returning "id"
                                                                     ^

我已经尝试将 $primary_key 设置为 null 和数据透视表中的其中一个 id 列 table 但它总是 returns 这个错误。

显然,要避免在 heroku 中播种数据透视表时出现此类问题,您可以使用 insert() 函数而不是 create()insert() 没有 return 可能导致我得到的 id 列错误的 ID。