如何制作與“id”不同的id/主鍵列?當我嘗試它時,它會產生錯誤。
class CreateEmployeesTable extends Migration
{
// ...
public function up()
{
Schema::create('employees', function (Blueprint $table) {
$table->id('employee_id');
$table->string('first_name', 100);
$table->string('last_name', 100);
$table->string('email', 200);
$table->timestamps();
});
}
// ...
}
嘗試一下:
php artisan tinker
Psy Shell v0.10.8 (PHP 7.3.20 — cli) by Justin Hileman
>>> $employee = new App\Models\Employee
=> App\Models\Employee {#3452}
// ...
>>> $employee->save();
<warning>PHP Warning: oci_execute(): ORA-00904: "ID": invalid identifier in <path>Statement.php on line 159</warning>
Yajra\Pdo\Oci8\Exceptions\Oci8Exception with message 'Error Code : 904
Error Message : ORA-00904: "ID": invalid identifier
Position : 132
Statement : insert into "EMPLOYEES" ("FIRST_NAME", "LAST_NAME", "EMAIL", "UPDATED_AT", "CREATED_AT") values (:p0, :p1, :p2, :p3, :p4) returning "ID" into :p5
Bindings : [Roger,Rabbit,[email protected],2021-11-04 22:13:56,2021-11-04 22:13:56,0]
uj5u.com熱心網友回復:
當您在表中使用主鍵而不id需要在模型類中指定它時
class Employee extends Model
{
/**
* The primary key associated with the table.
*
* @var string
*/
protected $primaryKey = 'employee_id';
}
uj5u.com熱心網友回復:
id()
的ID的方法是一個別名的的bigIncrements方法。默認情況下,該方法會創建一個 id 列;$table->id();
bigIncrements()
bigIncrements 方法創建一個自動遞增的 UNSIGNED BIGINT(主鍵)等效列:
$table->bigIncrements('employee_id');
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/349324.html
下一篇:如何在一次遷移中運行遷移檔案夾
