Ở đây chúng tôi giải thích cách tạo khóa chính trong laravel. di chuyển khóa chính của laravel. Tạo một cột chuỗi thành khóa chính
Khóa chính của bảng là cột duy nhất của bảng, chúng ta có thể tạo khóa chính cả kiểu int và string
Tạo một di chuyển mới
php artisan make:migration create_orders_table
Thêm khóa chính tăng tự động
Theo mặc định, chúng tôi tạo id tên cột chính và cột tăng tự động của nó thuộc loại số nguyên lớn không dấu
Tôi hiện đang làm việc trên dự án Laravel 4 bao gồm một máy chủ chính và nhiều máy khách. Máy khách tạo dữ liệu và gửi dữ liệu này đến máy chủ chính. Để tránh xung đột, tôi đang sử dụng UUID v4 làm khóa chính
Tuy nhiên, khi dữ liệu được tạo trên máy chủ, tôi muốn chỉ định một số nguyên tăng tự động duy nhất để người dùng dễ dàng xác định dữ liệu hơn. Ví dụ. Thay vì nói về
public function up[]
{
Schema::table['items', function[$table]
{
$table->create[];
$table->string['id']->primary[]; //'id' For the purpose of keeping the ORM working, this field stores the UUID.
$table->integer['number', true]; //The human readable item number, the second parameter is true for auto-increment
$table->text['otherdata'];
$table->timestamps[];
}];
}
3, người dùng có thể nói về public function up[]
{
Schema::table['items', function[$table]
{
$table->create[];
$table->string['id']->primary[]; //'id' For the purpose of keeping the ORM working, this field stores the UUID.
$table->integer['number', true]; //The human readable item number, the second parameter is true for auto-increment
$table->text['otherdata'];
$table->timestamps[];
}];
}
4Để giữ cho ứng dụng của tôi có thể quản lý được, tôi đang sử dụng di chuyển, quá trình di chuyển hiện tại của tôi cho bảng này trông như thế này
public function up[]
{
Schema::table['items', function[$table]
{
$table->create[];
$table->string['id']->primary[]; //'id' For the purpose of keeping the ORM working, this field stores the UUID.
$table->integer['number', true]; //The human readable item number, the second parameter is true for auto-increment
$table->text['otherdata'];
$table->timestamps[];
}];
}
Vấn đề là Laravel tự động tạo khóa chính khi xác định tăng tự động và do đó quá trình di chuyển kết thúc không thành công vì có hai khóa chính
[Exception] SQLSTATE[42000]: Syntax error or access violation: 1068 Multiple primary key defined
[SQL: alter table `items` add primary key items_id_primary[`id`]] [Bindings: array []]
Có cách nào để có một bảng có khóa chính và trường tăng tự động riêng biệt bằng cách sử dụng di chuyển Laravel 4 không
Xin chào @tim-o, đây là các ví dụ khác nhau mà bạn đã yêu cầu - với cả mã trình tạo lược đồ của Laravel do quá trình di chuyển chạy và SQL tương đương được tạo và gửi tới CRDB;
Ghi chú. Tôi đang sử dụng Laravel v5. 6
1. public function up[]
{
Schema::table['items', function[$table]
{
$table->create[];
$table->string['id']->primary[]; //'id' For the purpose of keeping the ORM working, this field stores the UUID.
$table->integer['number', true]; //The human readable item number, the second parameter is true for auto-increment
$table->text['otherdata'];
$table->timestamps[];
}];
}
5
public function up[]
{
Schema::table['items', function[$table]
{
$table->create[];
$table->string['id']->primary[]; //'id' For the purpose of keeping the ORM working, this field stores the UUID.
$table->integer['number', true]; //The human readable item number, the second parameter is true for auto-increment
$table->text['otherdata'];
$table->timestamps[];
}];
}
Lược đồ di chuyển
Schema::create['users', function [Blueprint $table] {
$table->increments['id'];
$table->string['name'];
$table->string['email']->unique[];
$table->string['password'];
$table->rememberToken[];
$table->timestamps[];
}];
SQL được tạo
CreateUsersTable: create table "users" ["id" serial primary key not null, "name" varchar[255] not null, "email" varchar[255] not null, "password" varchar[255] not null, "remember_token" varchar[100] null, "created_at" timestamp[0] without time zone null, "updated_at" timestamp[0] without time zone null]
CreateUsersTable: alter table "users" add constraint "users_email_unique" unique ["email"]
2. public function up[]
{
Schema::table['items', function[$table]
{
$table->create[];
$table->string['id']->primary[]; //'id' For the purpose of keeping the ORM working, this field stores the UUID.
$table->integer['number', true]; //The human readable item number, the second parameter is true for auto-increment
$table->text['otherdata'];
$table->timestamps[];
}];
}
6
public function up[]
{
Schema::table['items', function[$table]
{
$table->create[];
$table->string['id']->primary[]; //'id' For the purpose of keeping the ORM working, this field stores the UUID.
$table->integer['number', true]; //The human readable item number, the second parameter is true for auto-increment
$table->text['otherdata'];
$table->timestamps[];
}];
}
Lược đồ di chuyển
Schema::create['users', function [Blueprint $table] {
$table->bigIncrements['id'];
$table->string['name'];
$table->string['email']->unique[];
$table->string['password'];
$table->rememberToken[];
$table->timestamps[];
}];
SQL được tạo
CreateUsersTable: create table "users" ["id" bigserial primary key not null, "name" varchar[255] not null, "email" varchar[255] not null, "password" varchar[255] not null, "remember_token" varchar[100] null, "created_at" timestamp[0] without time zone null, "updated_at" timestamp[0] without time zone null]
CreateUsersTable: alter table "users" add constraint "users_email_unique" unique ["email"]
3. CreateUsersTable: create table "users" ["id" serial primary key not null, "name" varchar[255] not null, "email" varchar[255] not null, "password" varchar[255] not null, "remember_token" varchar[100] null, "created_at" timestamp[0] without time zone null, "updated_at" timestamp[0] without time zone null]
CreateUsersTable: alter table "users" add constraint "users_email_unique" unique ["email"]
0
CreateUsersTable: create table "users" ["id" serial primary key not null, "name" varchar[255] not null, "email" varchar[255] not null, "password" varchar[255] not null, "remember_token" varchar[100] null, "created_at" timestamp[0] without time zone null, "updated_at" timestamp[0] without time zone null]
CreateUsersTable: alter table "users" add constraint "users_email_unique" unique ["email"]
Lược đồ di chuyển
________số 8SQL được tạo
CreateUsersTable: create table "users" ["id" varchar[255] not null, "name" varchar[255] not null, "email" varchar[255] not null, "password" varchar[255] not null, "remember_token" varchar[100] null, "created_at" timestamp[0] without time zone null, "updated_at" timestamp[0] without time zone null]
CreateUsersTable: alter table "users" add primary key ["id"]
CreateUsersTable: alter table "users" add constraint "users_email_unique" unique ["email"]
4. CreateUsersTable: create table "users" ["id" serial primary key not null, "name" varchar[255] not null, "email" varchar[255] not null, "password" varchar[255] not null, "remember_token" varchar[100] null, "created_at" timestamp[0] without time zone null, "updated_at" timestamp[0] without time zone null]
CreateUsersTable: alter table "users" add constraint "users_email_unique" unique ["email"]
1
CreateUsersTable: create table "users" ["id" serial primary key not null, "name" varchar[255] not null, "email" varchar[255] not null, "password" varchar[255] not null, "remember_token" varchar[100] null, "created_at" timestamp[0] without time zone null, "updated_at" timestamp[0] without time zone null]
CreateUsersTable: alter table "users" add constraint "users_email_unique" unique ["email"]
Lược đồ di chuyển
public function up[]
{
Schema::table['items', function[$table]
{
$table->create[];
$table->string['id']->primary[]; //'id' For the purpose of keeping the ORM working, this field stores the UUID.
$table->integer['number', true]; //The human readable item number, the second parameter is true for auto-increment
$table->text['otherdata'];
$table->timestamps[];
}];
}
1SQL được tạo
public function up[]
{
Schema::table['items', function[$table]
{
$table->create[];
$table->string['id']->primary[]; //'id' For the purpose of keeping the ORM working, this field stores the UUID.
$table->integer['number', true]; //The human readable item number, the second parameter is true for auto-increment
$table->text['otherdata'];
$table->timestamps[];
}];
}
2