這是我的第一個表遷移檔案:
public function up()
{
Schema::create('produits', function (Blueprint $table) {
$table->id();
$table->string('nom',255)->unique();
$table->string('photo')->default('default.png');
$table->longText('description');
$table->float('prix');
$table->unsignedBigInteger('categorie_id');
$table->foreign('categorie_id')->refrences('id')->on('categories')->onDelete('cascade');
$table->timestamps();
});
}
這是我的第二個表遷移檔案:
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('nom',255)->unique();
$table->timestamps();
});
}
這是我得到的錯誤:
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'produits' already exists (SQL: create table `produits` (`id` bigint unsigned not null auto_increment primary key, `nom` varchar(255) not null, `photo` varchar(255) not null default 'default.png', `description` longtext not null, `prix` double(8, 2) not null, `categorie_id` bigint unsigned not null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')
我都試過了: php artisan migrate,php artisan migrate:refresh
uj5u.com熱心網友回復:
您可以嘗試從資料庫中洗掉表,在再次運行遷移之前,請確保首先創建類別遷移。如果您先創建了produits遷移,則它不會起作用,因為在運行遷移時,會先創建produits表,它不會找到尚不存在的參考表。因此,不會創建外鍵約束。
uj5u.com熱心網友回復:
幾乎,您可以嘗試php artisan migrate:fresh,這將洗掉所有表并創建新表。
你的遷移有 drop 方法嗎?
public function down()
{
statement('DROP TABLE categories');
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/425330.html
