问题描述
我正在尝试构建 Laravel 8 CRUD 应用程序。
这是我的迁移文件,2021_06_29_201031_create_books_table.PHP
<?PHP
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateBooksTable extends Migration
{
public function up()
{
Schema::create('books',function (Blueprint $table) {
$table->string('book_title',100)->primary();
$table->integer('book_pages');
$table->integer('book_year_published',4);
});
}
public function down()
{
Schema::dropIfExists('books');
}
}
<?PHP
namespace App\Models;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
class Book extends Model
{
protected $table = 'books';
protected $primaryKey = 'book_title';
public $incrementing = false;
protected $keyType = 'string';
public $timestamps = false;
protected $casts = [
'book_pages' => 'int','book_year_published' => 'int'
];
protected $fillable = [
'book_pages','book_year_published'
];
}
Illuminate\Database\QueryException
sqlSTATE[42000]: Syntax error or access violation: 1068 Multiple primary key defined (sql: alter table `books` add primary key `books_book_title_primary`(`book_title`))
at C:\xampp\htdocs\myapp\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 C:\xampp\htdocs\myapp\vendor\laravel\framework\src\Illuminate\Database\Connection.PHP:485
PDOException::("sqlSTATE[42000]: Syntax error or access violation: 1068 Multiple primary key defined")
2 C:\xampp\htdocs\myapp\vendor\laravel\framework\src\Illuminate\Database\Connection.PHP:485
PDOStatement::execute()
我已经看过这些答案,但没有一个有效:
- How to disable Laravel eloquent Auto Increment?
- Syntax error or access violation: 1068 Multiple primary key defined
- defining primary keys in migration
CREATE TABLE books(
book_title VARCHAR(100) PRIMARY KEY,book_pages INT NOT NULL,book_year_published INT(4) NOT NULL
);
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)