【问题标题】:Laravel 6 Database Seed problem: Table doesn't existLaravel 6 数据库种子问题:表不存在
【发布时间】:2020-03-31 14:09:19
【问题描述】:

我正在设计一个允许用户发布、评论和编辑书籍的网站。我目前正在处理每个用户的角色,并且遇到了向 user_role 播种的问题。我遇到了以下错误,但不知道为什么。

唯一的假设是它在某个地方被命名为 role_user,但看不到在哪里或为什么?

   Illuminate\Database\QueryException  : SQLSTATE[42S02]: Base table or view not found: 1146 Table 'dockerphp.role_user' doesn't exist (SQL: insert into `role_user` (`role_id`, `user_id`) values (1, 1))

  at /srv/app/vendor/laravel/framework/src/Illuminate/Database/Connection.php:669
    665|         // If an exception occurs when attempting to run a query, we'll format the error
    666|         // message to include the bindings with SQL, which will make this exception a
    667|         // lot more helpful to the developer instead of just the database's errors.
    668|         catch (Exception $e) {
  > 669|             throw new QueryException(
    670|                 $query, $this->prepareBindings($bindings), $e
    671|             );
    672|         }
    673|

  Exception trace:

  1   PDOException::("SQLSTATE[42S02]: Base table or view not found: 1146 Table 'dockerphp.role_user' doesn't exist")
      /srv/app/vendor/laravel/framework/src/Illuminate/Database/Connection.php:457

  2   PDO::prepare("insert into `role_user` (`role_id`, `user_id`) values (?, ?)")
      /srv/app/vendor/laravel/framework/src/Illuminate/Database/Connection.php:457

我的播种机是这样的:

<?php

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
use App\User;
use App\Role;
class UsersTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        User::truncate();
        DB::table('user_role')->truncate();

        $authorRole = Role::where('name', 'Author')->first();
        $editorRole = Role::where('name', 'Editor')->first();
        $readerRole = Role::where('name', 'Reader')->first();

        $Author = User::create([
            'name' => 'Test Author',
            'email' => 'Test@author.com',
            'password' => Hash::make('testtest')
        ]);

        $Editor = User::create([
            'name' => 'Test Editor',
            'email' => 'Test@editor.com',
            'password' => Hash::make('testtest')
        ]);

        $Reader = User::create([
            'name' => 'Test Reader',
            'email' => 'Test@reader.com',
            'password' => Hash::make('testtest')
        ]);

        $Author->roles()->attach($authorRole);
        $Editor->roles()->attach($editorRole);
        $Reader->roles()->attach($readerRole);
    }
}

上面的那个似乎是遇到问题的那个,因为下面的播种机工作得很好。

class RolesTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Role::truncate();
        Role::create(['name' => 'Author']);
        Role::create(['name' => 'Editor']);
        Role::create(['name' => 'Reader']);
    }
}

最后,我的数据库播种器如下所示:

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        $this->call(RolesTableSeeder::class);
        $this->call(UsersTableSeeder::class);
    }
}
  • 角色模型如下:
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Role extends Model
{
    public function users(){
        return $this->belongsToMany('App\User');
    }
}

和用户模型

<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password', 'about', 'avatar' 
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function roles(){
        return $this->belongsToMany('App\Role');
    }
}

【问题讨论】:

  • 数据库中是否存在表?
  • 该表被称为 user_role,就像代码中概述的那样,这就是我无法理解此错误的原因。
  • 您是否为角色使用了外部包?默认数据透视表名称在 Laravel 中按字母顺序命名,因此 role_user 而不是 user_role
  • 您正在尝试插入表名 role_user 而不是 user_role
  • 但是代码中哪里有一个role_user表被引用它总是user_role,我注意到Alex Gholamian

标签: php laravel frameworks laravel-6


【解决方案1】:

问题在于 Laravel 按字母顺序命名关系,因此它被存储为 user_role,而是存储为 role_user。 通过更改两种模型来解决: 来自

public function roles(){
        return $this->belongsToMany('App\Role');
    }

public function roles(){
        return $this->belongsToMany('App\Role', 'user_role');
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-04
    • 2020-05-23
    • 1970-01-01
    • 2015-02-14
    • 2020-04-04
    • 2014-01-26
    • 2015-10-12
    相关资源
    最近更新 更多