laravel 为包含主键和外键的迁移文件运行“php artisan migrate”时出现语法错误或访问冲突错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27969630/
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
Syntax error or access violation error while running "php artisan migrate" for migration file containing primary & foreign keys
提问by 8yt3c0d3
Hi i am trying to create 2 tables in my DB named "brand" & "product". I have created migration file for "brand" called "create_brand" that contains:
嗨,我正在尝试在名为“品牌”和“产品”的数据库中创建 2 个表。我为名为“create_brand”的“品牌”创建了迁移文件,其中包含:
public function up()
{
Schema::create('brand', function($table){
$table->increments('brand_id');
$table->string('brand_name', 100);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('brand');
}
The migration file for "product" called "create_product" contains:
名为“create_product”的“产品”迁移文件包含:
public function up()
{
Schema::create('product', function($table){
$table->increments('skuid');
$table->integer('brand_id')->unasigned();
});
Schema::create('product', function($table){
$table->foreign('brand_id')->references('brand_id')->on('brand')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('product', function($table){
$table->dropForeign('brand_id');
});
Schema::drop('product');
}
Now when i run "php artisan migrate" i get the error:
现在,当我运行“php artisan migrate”时,出现错误:
[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error i
your SQL syntax; check the manual that corresponds to your MySQL server v
ersion for the right syntax to use near ') default character set utf8 colla
te utf8_unicode_ci' at line 1 (SQL: create table `product` () default chara
cter set utf8 collate utf8_unicode_ci)
[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error i
n your SQL syntax; check the manual that corresponds to your MySQL server v
ersion for the right syntax to use near ') default character set utf8 colla
te utf8_unicode_ci' at line 1
I would be really grateful if someone could help me solve this issue :) Thanks.
如果有人能帮我解决这个问题,我将不胜感激:) 谢谢。
回答by littlemissbot
Use Schema::create
just once. To edit the table use Schema::table
Schema::create
只需使用一次。编辑表格使用Schema::table
And when using foreign key the type should be unsignedInteger
. If you create a basic integer and try to put a foreign key on it, it will fail.
当使用外键时,类型应该是unsignedInteger
. 如果您创建一个基本整数并尝试在其上放置外键,它将失败。
Change your codes to
将您的代码更改为
public function up()
{
Schema::create('product', function($table){
$table->increments('skuid');
$table->unsignedInteger('brand_id');
});
Schema::table('product', function($table){
$table->foreign('brand_id')->references('brand_id')->on('brand');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('product');
}
回答by Asif Gujjar
For me the below code is works 100%.
In AppServiceProvider.php
对我来说,下面的代码是 100% 有效的。
在 AppServiceProvider.php 中
use Illuminate\Support\Facades\Schema;
public function boot()
{
Schema::defaultStringLength(191);
}