Laravel 5 Migrate 未找到基表或视图:1146
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35860280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5 Migrate Base table or view not found: 1146
提问by Mandy
I am in big problem. I am trying to rum php artisan migrate
to generate table migration, but i am getting
我遇到了大问题。我正在尝试朗姆酒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
[2016-03-08 05:49:01] local.ERROR: 异常 'PDOException' 带有消息 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'testing.permissions' 在 D: 中不存在\xampp\htdocs\LMS-testing\vendor\laravel\framework\src\Illuminate\Database\Connection.php:333
error. I have tried and search on internet, where i have mistaken, but i did not get any solution.
错误。我已经尝试并在互联网上搜索,我弄错了,但我没有得到任何解决方案。
I have also tried Base table or view not found: 1146 Table Laravel 5and Doing a Laravel tutorial, getting "Base table or view not found: 1146 Table 'sdbd_todo.migrations' doesn't exist", but i did not get succeed.
我也试过Base table or view not found: 1146 Table Laravel 5和Doing a Laravel tutorial,得到“Base table or view not found: 1146 Table 'sdbd_todo.migrations' does not exist”,但我没有成功。
I have also tried to run php artisan list
, but again i am getting same error. I don't know why. Please suggest me the solution.
我也试过运行php artisan list
,但我再次遇到同样的错误。我不知道为什么。请给我建议解决方案。
Updated
更新
**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
回答by Vladimir Salguero
Check your migration file, maybe you are using Schema::table, like this:
检查您的迁移文件,也许您正在使用 Schema::table,如下所示:
Schema::table('table_name', function ($table) {
// ...
});
If you want to create a new table you must use Schema::create:
如果要创建新表,必须使用 Schema::create:
Schema::create('table_name', function ($table) {
// ...
});
Laracast More information in this link.
Laracast在此链接中有更多信息。
回答by Jino Antony
I ran it to a similar problem.
我把它跑到了类似的问题上。
Seems like I executed a Model query in the routes file. So it executes every time, even before running the actual migration.
好像我在路由文件中执行了一个模型查询。所以它每次都会执行,甚至在运行实际迁移之前。
//Problematic code
Route::view('users', 'users', [ 'users' => User::all() ]);
So I changed it to
所以我把它改成
Route::get('/users', function () {
return view('users', ['users' => User::all()]);
});
And everything works fine. Make sure that no Model queries is excecuted on the execution path and then try migrating.
一切正常。确保在执行路径上没有执行任何模型查询,然后尝试迁移。
回答by Dardanboy
I just had the same problem.
我只是遇到了同样的问题。
My solution was to comment what I had put into the boot
method of AppServiceProvider
(because in there I had Model request that didn't exist more).
我的解决方案是评论我在boot
方法中放入的内容AppServiceProvider
(因为在那里我有不存在的模型请求)。
回答by seyed morteza s oskooei
I had this problem when I tried to migrate new database. I had a function in AuthServiceProvider which selects all permissions. I commented that function and the problem was fixed.
当我尝试迁移新数据库时遇到了这个问题。我在 AuthServiceProvider 中有一个选择所有权限的功能。我评论了那个功能,问题就解决了。
回答by Mo D Genesis
The problem is that the foreign keys are added but cannot find the table because it hasn't been created.
问题是添加了外键但找不到表,因为它尚未创建。
1) make tables without foreign keys
1)制作没有外键的表
2) Make a 9999_99_99_999999_create_foreign_keys.php file
2) 制作一个 9999_99_99_999999_create_foreign_keys.php 文件
3) put there the foreign keys you want. With the 999..9 .php file it makes sure that it does the foreign keys last after the tables have been made.
3)把你想要的外键放在那里。使用 999..9 .php 文件,它确保在制作表后最后执行外键。
4) You have added the tables first and then added the foreign keys. This will work.
4) 您先添加了表,然后添加了外键。这将起作用。
回答by Cesar Devesa
It worked for me, see more here:
它对我有用,请在此处查看更多信息:
https://stackoverflow.com/a/49300262/5129122
https://stackoverflow.com/a/49300262/5129122
try {
return Permission::with('role')->get();
} catch (\Exception $e) {
return [];
}
回答by Artenes Nogueira
Let's look at the error message:
让我们看一下错误信息:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'testing.permissions' doesn't exist
So the table permissions
doesn't exist. Now let's look at the migrations:
所以表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']);
});
We can see in this Schema
call that you are defining a foreign key to the permissions
table.
我们可以在此Schema
调用中看到您正在为permissions
表定义外键。
With this, we can assume that Laravel is trying to create this foreign key, but the table that it wants to refer to doesn't exist.
有了这个,我们可以假设 Laravel 正在尝试创建这个外键,但是它想要引用的表不存在。
This usually happens when the order of the migration files is not equal the order in wich the tables must be created. So, if I have the migrations files in this order in my migrations
folder:
当迁移文件的顺序与必须创建表的顺序不相等时,通常会发生这种情况。因此,如果我的文件migrations
夹中有按此顺序的迁移文件:
2016_10_09_134416_create_permission_role_table
2016_10_09_134416_create_permissions_table
They will be executed in that order. But if I know that permission_role
dependes on permissions
, I need to change their positions by changing the timestamp in their file names:
它们将按该顺序执行。但是,如果我知道这permission_role
取决于permissions
,我需要通过更改文件名中的时间戳来更改它们的位置:
2016_10_09_134415_create_permissions_table
2016_10_09_134416_create_permission_role_table
In this case I've changed only the last digit from create_permissions_table
so it will be less than the timestamp from create_permissions_role_table
. After this, don't forget to run:
在这种情况下,我只更改了最后一位数字,create_permissions_table
因此它将小于create_permissions_role_table
. 在此之后,不要忘记运行:
composer dump-autoload
So composer can be aware of your change.
所以作曲家可以知道你的变化。
回答by Phantom1412
For those who end up being here because of the "migrations" table is not created automatically. In some cases you need to run the following artisan command:
对于那些因为“迁移”表不会自动创建而最终来到这里的人。在某些情况下,您需要运行以下 artisan 命令:
php artisan migrate:install
OR with code:
或使用代码:
Artisan::call('migrate:install', [
'--database' => 'your_database_connection', // optional
]);
to get your base "migrations" table, not sure why it is not generated automatically but it does happened.
要获取您的基本“迁移”表,不确定为什么它不会自动生成但确实发生了。
回答by ffuentes
The only solution that worked for me was to disable PermissionsServiceProvider in config/app.php before migrating.
唯一对我有用的解决方案是在迁移之前禁用 config/app.php 中的 PermissionsServiceProvider。
回答by Andy
In case anybody else runs in to this I just had the same issue and the reason it was happening was because I had created several commands and then needed to rollback my db to rerun the migrations.
万一其他人遇到这个问题,我只是遇到了同样的问题,它发生的原因是因为我创建了几个命令,然后需要回滚我的数据库以重新运行迁移。
To fix it I had to comment out the contents of the
为了修复它,我不得不注释掉
protected $commands = []
in app\Console\Kernel.php file.
在 app\Console\Kernel.php 文件中。
Phew!!!! :-)
呼!!!!:-)