Laravel tạo di chuyển cho mô hình hiện có

Trong khi, các phương pháp

Schema::create['table_name', function [Blueprint $table] {
    $table->increments['id'];
    $table->string['field_2'];
    $table->string['field_3'];
    $table->string['field_4'];
    $table->date['field_5'];
    $table->string['field_6'];
    $table->timestamps[];
}];
6 và
Schema::create['table_name', function [Blueprint $table] {
    $table->increments['id'];
    $table->string['field_2'];
    $table->string['field_3'];
    $table->string['field_4'];
    $table->date['field_5'];
    $table->string['field_6'];
    $table->timestamps[];
}];
7 vẫn được áp dụng, Rails 3. 1 nhận được một phương thức
Schema::create['table_name', function [Blueprint $table] {
    $table->increments['id'];
    $table->string['field_2'];
    $table->string['field_3'];
    $table->string['field_4'];
    $table->date['field_5'];
    $table->string['field_6'];
    $table->timestamps[];
}];
8 "biết cách di chuyển cơ sở dữ liệu của bạn và đảo ngược nó khi quá trình di chuyển được khôi phục mà không cần phải viết một phương thức riêng"

Xem "Di chuyển bản ghi đang hoạt động" để biết thêm thông tin

rails g migration FixColumnName

class FixColumnName < ActiveRecord::Migration
  def change
    rename_column :table_name, :old_column, :new_column
  end
end

Nếu bạn tình cờ có cả đống cột cần đổi tên hoặc thứ gì đó yêu cầu lặp đi lặp lại tên bảng

rename_column :table_name, :old_column1, :new_column1
rename_column :table_name, :old_column2, :new_column2
...

Bạn có thể sử dụng

Schema::create['table_name', function [Blueprint $table] {
    $table->increments['id'];
    $table->string['field_2'];
    $table->string['field_3'];
    $table->string['field_4'];
    $table->date['field_5'];
    $table->string['field_6'];
    $table->timestamps[];
}];
9 để giữ mọi thứ gọn gàng hơn

class FixColumnNames < ActiveRecord::Migration
  def change
    change_table :table_name do |t|
      t.rename :old_column1, :new_column1
      t.rename :old_column2, :new_column2
      ...
    end
  end
end

Sau đó, chỉ cần

class RenameOldTableToNewTable < ActiveRecord::Migration
  def self.up
    rename_table :old_table_name, :new_table_name
  end

  def self.down
    rename_table :new_table_name, :old_table_name
  end
end
0 như bình thường hoặc bất cứ điều gì bạn tiếp tục công việc kinh doanh của mình

Đối với đường ray 4

Trong khi tạo một

class RenameOldTableToNewTable < ActiveRecord::Migration
  def self.up
    rename_table :old_table_name, :new_table_name
  end

  def self.down
    rename_table :new_table_name, :old_table_name
  end
end
1 để đổi tên một cột, Rails 4 tạo ra một phương thức
Schema::create['table_name', function [Blueprint $table] {
    $table->increments['id'];
    $table->string['field_2'];
    $table->string['field_3'];
    $table->string['field_4'];
    $table->date['field_5'];
    $table->string['field_6'];
    $table->timestamps[];
}];
8 thay vì
Schema::create['table_name', function [Blueprint $table] {
    $table->increments['id'];
    $table->string['field_2'];
    $table->string['field_3'];
    $table->string['field_4'];
    $table->date['field_5'];
    $table->string['field_6'];
    $table->timestamps[];
}];
6 và
Schema::create['table_name', function [Blueprint $table] {
    $table->increments['id'];
    $table->string['field_2'];
    $table->string['field_3'];
    $table->string['field_4'];
    $table->date['field_5'];
    $table->string['field_6'];
    $table->timestamps[];
}];
7 như đã đề cập trong phần trên. Phương thức
Schema::create['table_name', function [Blueprint $table] {
    $table->increments['id'];
    $table->string['field_2'];
    $table->string['field_3'];
    $table->string['field_4'];
    $table->date['field_5'];
    $table->string['field_6'];
    $table->timestamps[];
}];
8 được tạo là

Trình tạo di chuyển cho Laravel là một gói của Bennett Treptow để tạo di chuyển từ các cấu trúc cơ sở dữ liệu hiện có

Trường hợp sử dụng chính cho gói này sẽ là một dự án có nhiều lần di chuyển làm thay đổi các bảng bằng cách sử dụng ->change[] từ học thuyết/dbal mà SQLite không hỗ trợ và cần một cách để cập nhật cấu trúc bảng cho SQLite để sử dụng trong các thử nghiệm. Một trường hợp sử dụng khác là lấy một dự án có cơ sở dữ liệu và không có di chuyển và biến cơ sở dữ liệu đó thành di chuyển cơ sở

Gói này có thể hữu ích nếu bạn đang chuyển một ứng dụng hiện có sang Laravel và muốn tạo lại quá trình di chuyển cơ sở dữ liệu cho ứng dụng để trợ giúp phát triển và thử nghiệm

Liên quan Tạo Password Generator với Laravel

Để hình dung quá trình này hoạt động như thế nào, readme có cách sử dụng ví dụ xác định bảng

Schema::create['table_name', function [Blueprint $table] {
    $table->increments['id'];
    $table->string['field_2'];
    $table->string['field_3'];
    $table->string['field_4'];
    $table->date['field_5'];
    $table->string['field_6'];
    $table->timestamps[];
}];
7 sau

Schema::create['table_name', function [Blueprint $table] {
    $table->increments['id'];
    $table->string['field_2'];
    $table->string['field_3'];
    $table->string['field_4'];
    $table->date['field_5'];
    $table->string['field_6'];
    $table->timestamps[];
}];
3

Sử dụng gói này, bạn có thể chạy lệnh sau để tạo lớp bản thiết kế dựa trên định nghĩa bảng

Schema::create['table_name', function [Blueprint $table] {
    $table->increments['id'];
    $table->string['field_2'];
    $table->string['field_3'];
    $table->string['field_4'];
    $table->date['field_5'];
    $table->string['field_6'];
    $table->timestamps[];
}];
4

Và kế hoạch chi tiết dẫn xuất từ ​​bảng

Schema::create['table_name', function [Blueprint $table] {
    $table->increments['id'];
    $table->string['field_2'];
    $table->string['field_3'];
    $table->string['field_4'];
    $table->date['field_5'];
    $table->string['field_6'];
    $table->timestamps[];
}];
7 sẽ trông như sau theo ví dụ

Schema::create['table_name', function [Blueprint $table] {
    $table->increments['id'];
    $table->string['field_2'];
    $table->string['field_3'];
    $table->string['field_4'];
    $table->date['field_5'];
    $table->string['field_6'];
    $table->timestamps[];
}];
5

Gói này cũng đi kèm với các sơ đồ di chuyển bảng và xem khác nhau và cài đặt cấu hình. Ví dụ: một cấu hình xác định các mẫu tên tệp được sử dụng để tạo lược đồ bảng

Schema::create['table_name', function [Blueprint $table] {
    $table->increments['id'];
    $table->string['field_2'];
    $table->string['field_3'];
    $table->string['field_4'];
    $table->date['field_5'];
    $table->string['field_6'];
    $table->timestamps[];
}];
6

Tại thời điểm viết gói hỗ trợ MySQL, nhưng cũng có thể hỗ trợ Postgres, SQLite và SQL Server theo readme

Bạn có thể tìm hiểu thêm về gói này, nhận hướng dẫn cài đặt đầy đủ và xem mã nguồn trên GitHub

Gói này đã được gửi đến phần Liên kết Tin tức Laravel của chúng tôi. Liên kết là nơi cộng đồng có thể đăng các gói và hướng dẫn xung quanh hệ sinh thái Laravel. Theo dõi trên Twitter @LaravelLinks

Làm cách nào để tạo tệp di chuyển cho một mô hình trong Laravel?

Để tạo một lần di chuyển mới, bạn có thể chạy make. migration Lệnh Artisan và lệnh đó sẽ khởi động một lớp mới trên ứng dụng Laravel của bạn, trong thư mục cơ sở dữ liệu/di chuyển. Lớp này sẽ chứa mã soạn sẵn mặc định.

Làm cách nào để tạo di chuyển từ cơ sở dữ liệu hiện có trong Laravel?

Chạy di chuyển Laravel trên cơ sở dữ liệu hiện có .
Mục lục. Mục lục. .
Tổng quan ngắn gọn. Khi chúng ta muốn tạo một bảng cơ sở dữ liệu trong Laravel, trước tiên chúng ta cần tạo mô hình của bảng. .
điều kiện tiên quyết. .
Bắt đầu. .
Sửa đổi tập tin mô hình. .
Sửa đổi tệp di chuyển. .
Chạy di chuyển. .
Tóm lược

Làm cách nào để di chuyển trong Laravel mà không mất dữ liệu?

Đặt chi tiết cột của bạn ở đó, chạy di chuyển bằng cách sử dụng di chuyển thủ công php và đó là tất cả. Bạn sẽ có cột mới này trong bảng người dùng của mình mà không làm mất dữ liệu đã lưu trữ trước đó

Làm cách nào để cập nhật tệp di chuyển hiện có trong Laravel?

Bạn chỉ cần chạy di chuyển thủ công. rollback hoặc artisan migration. làm mới để khôi phục nhóm di chuyển cuối cùng hoặc bắt đầu lại từ đầu tương ứng.

Chủ Đề