“php artisan db:seed”命令有兩個小問題。
- 當我運行命令時,我有這個錯誤訊息:
“SQLSTATE [42S02] 未找到基表或視圖:1146 La 表”bootstrap_template_commerciaux es “n'existe pas ...”
問題是:我的表名是commerciaux,而不是commerciauxes。我檢查了所有檔案,我的模型是 Commerciaux.php,我的工廠 CommerciauxFactory。那么……這是什么魔法?我錯過了什么?
其次,來自 db:seed 的 SQL 請求添加了一些我不想添加的列:
SQLSTATE [42S02]:未找到基表或視圖:1146 La table 'bootstrap_template.commerciauxes' n'existe pas(SQL:插入
commerciauxes(nom、、、、updated_atprenom、created_at)值(Luis Champlin 博士、Luella Leuschkeville博士、利薩伯格, 2022-06-03 21:42:44, 2022-06-03 21:42:44))
這是我的 Commerciaux 模型:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Commerciaux extends Model
{
use HasFactory;
protected $fillable = [
'nom',
'prenom',
'ville',
'nbre_commande',
];
}
我的商業工廠(以防萬一)
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
class CommerciauxFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'nom' => $this->faker->name(),
'prenom' => $this->faker->name(),
'ville' => $this->faker->city(),
];
}
}
非常感謝您抽出寶貴的時間,我想嘗試這個不錯的工具,但由于這些錯誤我被阻止了 2 天。
uj5u.com熱心網友回復:
要回答您的問題:
由于默認轉換,Laravel 默認將表名視為復數,我建議保持這種方式。如果您想定義自己的表名,那么在您的模型類中,您可以為您的表名定義以下內容:
受保護的 $table = 'commerciaux';
此外,在遷移的 Up 函式中,將表名設定如下:
public function up()
{
Schema::create('commerciaux', function (Blueprint $table) {
//Your table columns and structure
});
}
關于其他列,這些是 laravel 時間戳,用于跟蹤記錄在上次創建 (
created_at) 和更新( ) 時的時間戳。updated_at在這種情況下,我還建議保留這些欄位,因為它們會跟蹤記錄創建和上次修改時間戳。如果您不希望表中出現這些欄位,則可以在模型中定義以下代碼以排除時間戳:公共 $timestamps = 假;
除此之外,您還可以從遷移中洗掉以下行:
table->timestamps();
編輯:再次運行遷移之前,請嘗試回滾命令,以便可以從遷移表中洗掉創建的基表和遷移記錄。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/485948.html
標籤:sql 拉拉维尔 laravel 工匠 laravel 播种
下一篇:Laravel9和Livewire2多個storeAs方法拋出“在null上呼叫成員函式storeAs()”錯誤
