Php Laravel迁移后添加数据导致错误
我想在Php Laravel迁移后添加数据导致错误,php,laravel,Php,Laravel,我想在刷新或数据库迁移 public function up() { Schema::create('locations', function (Blueprint $table) { $table->bigIncrements('id'); $table->integer('type'); $table->integer('pid')->default(0);
刷新
或数据库
迁移
public function up()
{
Schema::create('locations', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('type');
$table->integer('pid')->default(0);
$table->integer('cid')->default(0);
$table->string('name');
$table->timestamps();
});
DB::table('locations')->insert([
['type' => 1, 'name' => 'Istanbul'],
['type' => 2, 'name' => 'Istanbul', 'pid' => 1],
['type' => 3, 'name' => 'Taksim', 'cid' => 2],
['type' => 3, 'name' => 'Beyoglu', 'cid' => 2],
]);
}
但它给了我一个错误:
?[41;1m\Database\QueryException?[49;22m:
?[33mSQLSTATE[42S01]:基础表或视图已存在:1050表
“位置”已存在(SQL:cre-ate表locations
(id
bigint无符号非空自动递增主键,type
int非空
空,pid
int-not-null默认值“0”,cid
int-not-null默认值“0”,
name
varchar(255)不为null,在时间戳null处创建,
已更新\u在
ti mestamp null)默认字符集utf8mb4比较
‘utf8mb4_unicode_ci’)
如何解决此问题?从迁移中删除DB:table
,然后运行:
php artisan迁移:刷新
然后
php artisan make:seed LocationTableSeeder
php artisan db:seed --class=LocationTableSeeder
然后将其添加到run()
函数:
DB::table('locations')->insert([
['type' => 1, 'name' => 'Istanbul', 'pid' => 0, 'cid' => 0, 'created_at' => Carbon::now()->format('Y-m-d H:i:s'), 'updated_at' => Carbon::now()->format('Y-m-d H:i:s')],
['type' => 2, 'name' => 'Istanbul', 'pid' => 1, 'cid' => 0, 'created_at' => Carbon::now()->format('Y-m-d H:i:s'), 'updated_at' => Carbon::now()->format('Y-m-d H:i:s')],
['type' => 3, 'name' => 'Taksim', 'cid' => 2, 'pid' => 0, 'created_at' => Carbon::now()->format('Y-m-d H:i:s'), 'updated_at' => Carbon::now()->format('Y-m-d H:i:s')],
['type' => 3, 'name' => 'Beyoglu', 'cid' => 2, 'pid' => 0, 'created_at' => Carbon::now()->format('Y-m-d H:i:s'), 'updated_at' => Carbon::now()->format('Y-m-d H:i:s')],
]);
然后
php artisan make:seed LocationTableSeeder
php artisan db:seed --class=LocationTableSeeder
不要将db insert放在迁移中使用db seed在表中添加数据。仍然会出现错误插入值列表与列列表不匹配:
@tourtravel错误非常清楚,您要插入的内容与列不匹配。添加pid
和cid
以及在处更新过时的答复