Laravel 5移植基础表或未找到视图:1146
我遇到了很大的问题。我想朗姆酒php artisan migrate
生成表迁移,但我得到 [2016-03-08 05:49:01] local.ERROR: exception 'PDOException' with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'testing.permissions' doesn't exist' in D:\xampp\htdocs\LMS-testing\vendor\laravel\framework\src\Illuminate\Database\Connection.php:333
错误。我曾尝试过在互联网上进行搜索,但我没有得到任何解决方案。Laravel 5移植基础表或未找到视图:1146
我也试过Base table or view not found: 1146 Table Laravel 5和Doing a Laravel tutorial, getting "Base table or view not found: 1146 Table 'sdbd_todo.migrations' doesn't exist",但我没有成功。
我也试过运行php artisan list
,但我又一次得到相同的错误。我不知道为什么。请给我解决方案。
更新
**RolesPermission migration table**
Schema::create('roles', function(Blueprint $table){
$table->increments('id');
$table->string('name')->unique();
$table->string('label');
$table->string('description')->nullable();
$table->timestamps();
});
Schema::create('permissions', function(Blueprint $table){
$table->increments('id');
$table->string('name')->unique();
$table->string('label');
$table->string('description')->nullable();
$table->timestamps();
});
Schema::create('permission_role', function(Blueprint $table){
$table->integer('permission_id')->unsigned();
$table->integer('role_id')->unsigned();
$table->foreign('permission_id')
->references('id')
->on('permissions')
->onDelete('cascade');
$table->foreign('role_id')
->references('id')
->on('roles')
->onDelete('cascade');
$table->primary(['permission_id', 'role_id']);
});
Schema::create('role_user', function(Blueprint $table){
$table->integer('role_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->foreign('role_id')
->references('id')
->on('roles')
->onDelete('cascade');
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
$table->primary(['role_id', 'user_id']);
});
.env file
APP_ENV=local
APP_DEBUG=true
APP_KEY=W8YWZe3LCngvZzexH3WLWqCDlYRSufuy
DB_HOST=127.0.0.1
DB_DATABASE=testing
DB_USERNAME=root
DB_PASSWORD=
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_DRIVER=log
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
让我们来看看以下错误消息:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'testing.permissions' doesn't exist
所以表permissions
不存在。现在,让我们看看迁移:
Schema::create('permission_role', function(Blueprint $table){
$table->integer('permission_id')->unsigned();
$table->integer('role_id')->unsigned();
$table->foreign('permission_id')
->references('id')
->on('permissions')
->onDelete('cascade');
$table->foreign('role_id')
->references('id')
->on('roles')
->onDelete('cascade');
$table->primary(['permission_id', 'role_id']);
});
我们可以在这个Schema
呼叫正在定义一个外键permissions
表中看到。
有了这个,我们可以假设Laravel试图创建这个外键,但它想要引用的表不存在。
这通常发生在迁移文件的顺序不等于必须创建表的顺序时。所以,如果我有我的migrations
文件夹按以下顺序迁移文件:
2016_10_09_134416_create_permission_role_table
2016_10_09_134416_create_permissions_table
他们将在顺序执行。但是,如果我知道,在permissions
permission_role
dependes,我需要改变它们的文件名中的时间戳来改变自己的立场:
2016_10_09_134415_create_permissions_table
2016_10_09_134416_create_permission_role_table
在这种情况下,我已经改变了只从create_permissions_table
最后一位,因此会少比从create_permissions_role_table
的时间戳。在此之后,不要忘记跑步:
composer dump-autoload
所以作曲家可以知道你的改变。
问题是外键已添加,但因为尚未创建而无法找到该表。
1)使表,而不用外键
2)做一个9999_99_99_999999_create_foreign_keys.php文件
3)把有需要的外键。使用999..9 .php文件可以确保它在完成表后创建外键。
4)你首先添加的表,然后添加外键。这将工作。
万一别人运行在此我有同样的问题,它发生的原因是因为我已经创建了几个命令,然后回滚我的数据库重新运行迁移需要。
为了解决这个问题,我不得不注释掉的
protected $commands = []
内容的应用程序\控制台\ Kernel.php文件。
唷!!!! :-)
能否请您分享迁移文件的代码? –
已更新,请检查。 – Mandy
即使我无法运行php artisan make或任何其他命令。我在创建控制器或模型时遇到同样的错误。 – Mandy