运行 "php artisan migrate" 包含主键和外键的迁移文件出现语法错误或访问冲突错误
Syntax error or access violation error while running "php artisan migrate" for migration file containing primary & foreign keys
您好,我正在尝试在我的数据库中创建 2 个名为 "brand" 和 "product" 的表。我为 "brand" 创建了名为 "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');
}
"product" 的名为 "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');
}
现在当我 运行 "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
如果有人能帮我解决这个问题,我将不胜感激:) 谢谢。
只使用一次Schema::create
。要编辑 table 使用 Schema::table
并且当使用外键时类型应该是unsignedInteger
。如果您创建一个基本整数并尝试在其上放置外键,它将失败。
将您的代码更改为
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');
}
对我来说,下面的代码 100% 有效。
在AppServiceProvider.php
use Illuminate\Support\Facades\Schema;
public function boot()
{
Schema::defaultStringLength(191);
}
您好,我正在尝试在我的数据库中创建 2 个名为 "brand" 和 "product" 的表。我为 "brand" 创建了名为 "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');
}
"product" 的名为 "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');
}
现在当我 运行 "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
如果有人能帮我解决这个问题,我将不胜感激:) 谢谢。
只使用一次Schema::create
。要编辑 table 使用 Schema::table
并且当使用外键时类型应该是unsignedInteger
。如果您创建一个基本整数并尝试在其上放置外键,它将失败。
将您的代码更改为
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');
}
对我来说,下面的代码 100% 有效。
在AppServiceProvider.php
use Illuminate\Support\Facades\Schema;
public function boot()
{
Schema::defaultStringLength(191);
}