【发布时间】:2021-10-20 18:33:08
【问题描述】:
我正在学习 Laravel 8 并遇到了这个问题,我做了 4 个迁移及其模型。当我运行命令php artisan migrate时,上次迁移出现问题。
这是第一次迁移 (2021_08_18_092502_create_masyarakats_table.php)
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateMasyarakatsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('masyarakat', function (Blueprint $table) {
// Data akun masyarakat
$table->char('nik', 16)->primary();
$table->string('nama', 35);
$table->string('username', 25)->unique();
$table->string('password');
$table->string('telp', 13);
// Rekam tanggal
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('masyarakat');
}
}
这是第二次迁移 (2021_08_18_093833_create_pengaduans_table.php)
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePengaduansTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('pengaduan', function (Blueprint $table) {
// Data laporan pengaduan
$table->id('id_pengaduan', 11);
$table->dateTime('tgl_pengaduan');
$table->char('nik', 16);
$table->text('isi_laporan');
$table->string('foto');
$table->enum('status',['0', 'proses', 'selesai']);
// Rekam tanggal
$table->timestamps();
// Menghubungkan kolom
$table->foreign('nik')->references('nik')->on('masyarakat');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('pengaduan');
}
}
这是第三次迁移 (2021_08_19_002414_create_petugas_table.php)
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePetugasTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('petugas', function (Blueprint $table) {
// Data akun petugas
$table->id('id_petugas', 11);
$table->string('nama_petugas', 35);
$table->string('username', 25)->unique();
$table->string('password');
$table->string('telp', 13);
$table->enum('level', ['admin', 'selesai']);
// Rekam tanggal
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('petugas');
}
}
这是第四次迁移 (2021_08_19_003445_create_tanggapans_table.php)
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateTanggapansTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('tanggapan', function (Blueprint $table) {
// Data tanggapan laporan
$table->id('id_tanggapan', 11)->primary();
$table->integer('id_pengaduan', 11);
$table->dateTime('tgl_tanggapan');
$table->text('isi_tanggapan');
$table->integer('id_petugas', 11);
// Rekam tanggal
$table->timestamps();
// Menghubungkan kolom
$table->foreign('id_pengaduan')->references('id_pengaduan')->on('pengaduan');
$table->foreign('id_petugas')->references('id_petugas')->on('petugas');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('tanggapan');
}
}
对于模型。 这是第一个模型 (masyarakat.php)
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class masyarakat extends Model
{
use HasFactory;
protected $table = 'masyarakat';
protected $primaryKey = 'nik';
protected $fillable = [
'nik',
'nama',
'username',
'password',
'telp',
];
}
这是第二个模型 (pengaduan.php)
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class pengaduan extends Model
{
use HasFactory;
protected $table = 'pengaduan';
protected $primaryKey = 'id_pengaduan';
protected $fillable = [
'id_pengaduan',
'tgl_pengaduan',
'isi_laporan',
'nik',
'status',
'foto',
];
}
这是第三个模型 (petugas.php)
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class petugas extends Model
{
use HasFactory;
protected $table = 'petugas';
protected $primaryKey = 'id_petugas';
protected $fillable = [
'id_pengaduan',
'nama_petugas',
'username',
'password',
'level',
'telp',
];
}
这是第 4 个模型 (tanggapan.php)
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class tanggapan extends Model
{
use HasFactory;
protected $table = 'tanggapan';
protected $primaryKey = 'id_tanggapan';
protected $fillable = [
'tgl_pengaduan',
'isi_tanggapan',
'id_pengaduan',
'id_tanggapan',
];
}
遇到这个问题,前三迁移表创建成功,第四个迁移表失败。
迁移时的终端输出
Migrating: 2021_08_19_003445_create_tanggapans_table
Illuminate\Database\QueryException
SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only
one auto column and it must be defined as a key (SQL: create table `tanggapan` (`id_tanggapan` bigint unsigned not null auto_increment primary key, `id_pengaduan` int not null auto_increment primary key, `tgl_tanggapan` datetime not null, `isi_tanggapan` text not null, `id_petugas` int not null auto_increment primary key, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')
at D:\Docs\Sekolah\project_ukk_apriza\vendor\laravel\framework\src\Illuminate\Database\Connection.php:692
688▕ // If an exception occurs when attempting to run a query, we'll format the error
689▕ // message to include the bindings with SQL, which will make this exception a
690▕ // lot more helpful to the developer instead of just the database's errors.
691▕ catch (Exception $e) {
➜ 692▕ throw new QueryException(
693▕ $query, $this->prepareBindings($bindings), $e
694▕ );
695▕ }
696▕ }
1 D:\Docs\Sekolah\project_ukk_apriza\vendor\laravel\framework\src\Illuminate\Database\Connection.php:485
PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key")
2 D:\Docs\Sekolah\project_ukk_apriza\vendor\laravel\framework\src\Illuminate\Database\Connection.php:485
PDOStatement::execute()
请帮我解决这个问题。
【问题讨论】:
-
primary应该是primary()。如果你添加你得到的实际错误会有所帮助。 -
谢谢@P.K.Tharindu 我忘了。但是问题不存在,我已经编辑了问题,也许你可以帮忙。
标签: php database laravel model migration