跳至內容 跳至搜尋
方法
A
B
C
D
F
I
M
N
O
P
R
T
U
V

實例公開方法

add_belongs_to(table_name, ref_name, **options)

別名為:add_reference

add_check_constraint(table_name, expression, if_not_exists: false, **options)

新增新的檢查約束至表格。expression 是可驗證布林條件的 字串 表示。

add_check_constraint :products, "price > 0", name: "price_check"

產生

ALTER TABLE "products" ADD CONSTRAINT price_check CHECK (price > 0)

options hash 可包含下列金鑰

:name

約束名稱。預設為 chk_rails_<識別碼>

:if_not_exists

如果約束已存在,則靜默忽略,而不是引發錯誤。

:validate

(僅限 PostgreSQL)指定是否驗證約束。預設為 true

# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 1260
def add_check_constraint(table_name, expression, if_not_exists: false, **options)
  return unless supports_check_constraints?

  options = check_constraint_options(table_name, expression, options)
  return if if_not_exists && check_constraint_exists?(table_name, **options)

  at = create_alter_table(table_name)
  at.add_check_constraint(expression, options)

  execute schema_creation.accept(at)
end

add_column(table_name, column_name, type, **options)

新增一個名為 column_nametype 類型的欄位到 table_name

請參閱 ActiveRecord::ConnectionAdapters::TableDefinition.column

type 參數通常是遷移原生類型之一,如下列其中一項::primary_key:string:text:integer:bigint:float:decimal:numeric:datetime:time:date:binary:blob:boolean

只要資料庫支援,您就可以使用清單中沒有的類型(例如,MySQL 中的「多邊形」),但這將無法跨資料庫使用,通常應避免使用。

可用的選項如下(預設情況下,這些選項都不存在)

  • :comment - 指定欄位的註解。某些後端會忽略此選項。

  • :collation - 指定 :string:text 欄位的校對規則。如果未指定,欄位將與資料表使用相同的校對規則。

  • :default - 欄位的預設值。對 NULL 使用 nil

  • :limit - 要求最大欄位長度。這是 :string 欄位的字元數,以及 :text:binary:blob:integer 欄位的位元組數。某些後端會忽略此選項。

  • :null - 允許或不允許欄位中的 NULL 值。

  • :precision - 指定 :decimal:numeric:datetime:time 欄位的精度。

  • :scale - 指定 :decimal:numeric 欄位的縮放。

  • :if_not_exists - 指定如果欄位已存在,則不要嘗試重新新增。這將避免重複欄位錯誤。

注意:精度是有效數字的總數,縮放是小數點後可以儲存的數字數。例如,數字 123.45 的精度為 5,縮放為 2。精度為 5、縮放為 2 的小數範圍為 -999.99 到 999.99。

請注意不同 RDBMS 實作與 :decimal 欄位的行為

  • SQL 標準指出預設比例應為 0,:scale <= :precision,且未對 :precision 的需求提出說明。

  • MySQL::precision [1..65],:scale [0..30]。預設為 (10,0)。

  • PostgreSQL::precision [1..infinity],:scale [0..infinity]。無預設值。

  • SQLite3:對 :precision:scale 沒有限制,但支援的最大 :precision 為 16。無預設值。

  • Oracle::precision [1..38],:scale [-84..127]。預設為 (38,0)。

  • SqlServer::precision [1..38],:scale [0..38]。預設為 (38,0)。

範例

add_column(:users, :picture, :binary, limit: 2.megabytes)
# ALTER TABLE "users" ADD "picture" blob(2097152)

add_column(:articles, :status, :string, limit: 20, default: 'draft', null: false)
# ALTER TABLE "articles" ADD "status" varchar(20) DEFAULT 'draft' NOT NULL

add_column(:answers, :bill_gates_money, :decimal, precision: 15, scale: 2)
# ALTER TABLE "answers" ADD "bill_gates_money" decimal(15,2)

add_column(:measurements, :sensor_reading, :decimal, precision: 30, scale: 20)
# ALTER TABLE "measurements" ADD "sensor_reading" decimal(30,20)

# While :scale defaults to zero on most databases, it
# probably wouldn't hurt to include it.
add_column(:measurements, :huge_integer, :decimal, precision: 30)
# ALTER TABLE "measurements" ADD "huge_integer" decimal(30)

# Defines a column that stores an array of a type.
add_column(:users, :skills, :text, array: true)
# ALTER TABLE "users" ADD "skills" text[]

# Defines a column with a database-specific type.
add_column(:shapes, :triangle, 'polygon')
# ALTER TABLE "shapes" ADD "triangle" polygon

# Ignores the method call if the column exists
add_column(:shapes, :triangle, 'polygon', if_not_exists: true)
# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 620
def add_column(table_name, column_name, type, **options)
  add_column_def = build_add_column_definition(table_name, column_name, type, **options)
  return unless add_column_def

  execute schema_creation.accept(add_column_def)
end

add_foreign_key(from_table, to_table, **options)

新增一個外來鍵。from_table 是具有鍵欄位的表格,to_table 包含參照的主鍵。

外來鍵將以下列模式命名:fk_rails_<identifier>identifier 是從 from_tablecolumn 確定產生的 10 個字元長字串。可以透過 :name 選項指定自訂名稱。

建立一個簡單的外來鍵
add_foreign_key :articles, :authors

產生

ALTER TABLE "articles" ADD CONSTRAINT fk_rails_e74ce85cbc FOREIGN KEY ("author_id") REFERENCES "authors" ("id")
建立一個外來鍵,如果外來鍵已存在,則忽略方法呼叫
add_foreign_key(:articles, :authors, if_not_exists: true)
在特定欄位上建立一個外來鍵
add_foreign_key :articles, :users, column: :author_id, primary_key: "lng_id"

產生

ALTER TABLE "articles" ADD CONSTRAINT fk_rails_58ca3d3a82 FOREIGN KEY ("author_id") REFERENCES "users" ("lng_id")
建立一個複合外來鍵
Assuming "carts" table has "(shop_id, user_id)" as a primary key.

add_foreign_key :orders, :carts, primary_key: [:shop_id, :user_id]

產生

ALTER TABLE "orders" ADD CONSTRAINT fk_rails_6f5e4cb3a4 FOREIGN KEY ("cart_shop_id", "cart_user_id") REFERENCES "carts" ("shop_id", "user_id")
建立一個串聯外來鍵
add_foreign_key :articles, :authors, on_delete: :cascade

產生

ALTER TABLE "articles" ADD CONSTRAINT fk_rails_e74ce85cbc FOREIGN KEY ("author_id") REFERENCES "authors" ("id") ON DELETE CASCADE

options hash 可包含下列金鑰

:column

from_table 上的外來鍵欄位名稱。預設為 to_table.singularize + "_id"。傳遞陣列以建立複合外來鍵。

:primary_key

to_table 上的主鍵欄位名稱。預設為 id。傳遞陣列以建立複合外來鍵。

:name

約束名稱。預設為 fk_rails_<identifier>

:on_delete

ON DELETE 發生時執行的動作。有效值為 :nullify:cascade:restrict

:on_update

ON UPDATE 發生時執行的動作。有效值為 :nullify:cascade:restrict

:if_not_exists

指定外來鍵是否已存在,以避免再次新增。這將避免重複欄位錯誤。

:validate

(僅限 PostgreSQL)指定是否驗證約束。預設為 true

:deferrable

(僅 PostgreSQL) 指定外來鍵是否可遞延。有效值為布林值或 :deferred:immediate 以指定預設行為。預設為 false

# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 1140
def add_foreign_key(from_table, to_table, **options)
  return unless use_foreign_keys?
  return if options[:if_not_exists] == true && foreign_key_exists?(from_table, to_table, **options.slice(:column))

  options = foreign_key_options(from_table, to_table, options)
  at = create_alter_table from_table
  at.add_foreign_key to_table, options

  execute schema_creation.accept(at)
end

add_index(table_name, column_name, **options)

新增一個索引至資料表。column_name 可以是單一 符號 或符號的 陣列

索引將以資料表和欄位名稱命名,除非您傳遞 :name 作為選項。

建立一個簡單索引
add_index(:suppliers, :name)

產生

CREATE INDEX index_suppliers_on_name ON suppliers(name)
建立一個已存在的索引
add_index(:suppliers, :name, if_not_exists: true)

產生

CREATE INDEX IF NOT EXISTS index_suppliers_on_name ON suppliers(name)

注意:MySQL 不支援。

建立一個唯一索引
add_index(:accounts, [:branch_id, :party_id], unique: true)

產生

CREATE UNIQUE INDEX index_accounts_on_branch_id_and_party_id ON accounts(branch_id, party_id)
建立一個命名索引
add_index(:accounts, [:branch_id, :party_id], unique: true, name: 'by_branch_party')

產生

CREATE UNIQUE INDEX by_branch_party ON accounts(branch_id, party_id)
建立一個具有特定鍵長度的索引
add_index(:accounts, :name, name: 'by_name', length: 10)

產生

CREATE INDEX by_name ON accounts(name(10))
建立一個具有多個鍵的特定鍵長度的索引
add_index(:accounts, [:name, :surname], name: 'by_name_surname', length: {name: 10, surname: 15})

產生

CREATE INDEX by_name_surname ON accounts(name(10), surname(15))

注意:僅 MySQL 支援。

建立一個具有排序順序的索引 (desc 或 asc,asc 為預設)
add_index(:accounts, [:branch_id, :party_id, :surname], name: 'by_branch_desc_party', order: {branch_id: :desc, party_id: :asc})

產生

CREATE INDEX by_branch_desc_party ON accounts(branch_id DESC, party_id ASC, surname)

注意:MySQL 僅支援 8.0.1 以後的索引順序 (較早版本接受語法但會忽略它)。

建立一個部分索引
add_index(:accounts, [:branch_id, :party_id], unique: true, where: "active")

產生

CREATE UNIQUE INDEX index_accounts_on_branch_id_and_party_id ON accounts(branch_id, party_id) WHERE active

注意:部分索引僅支援 PostgreSQL 和 SQLite。

建立一個包含其他欄位的索引
add_index(:accounts, :branch_id,  include: :party_id)

產生

CREATE INDEX index_accounts_on_branch_id ON accounts USING btree(branch_id) INCLUDE (party_id)

注意:僅 PostgreSQL 支援。

建立一個具有特定方法的索引
add_index(:developers, :name, using: 'btree')

產生

CREATE INDEX index_developers_on_name ON developers USING btree (name) -- PostgreSQL
CREATE INDEX index_developers_on_name USING btree ON developers (name) -- MySQL

注意:僅 PostgreSQL 和 MySQL 支援。

建立一個具有特定運算子類別的索引
add_index(:developers, :name, using: 'gist', opclass: :gist_trgm_ops)
# CREATE INDEX developers_on_name ON developers USING gist (name gist_trgm_ops) -- PostgreSQL

add_index(:developers, [:name, :city], using: 'gist', opclass: { city: :gist_trgm_ops })
# CREATE INDEX developers_on_name_and_city ON developers USING gist (name, city gist_trgm_ops) -- PostgreSQL

add_index(:developers, [:name, :city], using: 'gist', opclass: :gist_trgm_ops)
# CREATE INDEX developers_on_name_and_city ON developers USING gist (name gist_trgm_ops, city gist_trgm_ops) -- PostgreSQL

注意:僅 PostgreSQL 支援。

建立一個具有特定類型的索引
add_index(:developers, :name, type: :fulltext)

產生

CREATE FULLTEXT INDEX index_developers_on_name ON developers (name) -- MySQL

注意:僅 MySQL 支援。

建立一個具有特定演算法的索引
add_index(:developers, :name, algorithm: :concurrently)
# CREATE INDEX CONCURRENTLY developers_on_name on developers (name)

注意:僅 PostgreSQL 支援。

在交易中並行新增索引不受支援。

如需更多資訊,請參閱 「交易遷移」區段

# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 886
def add_index(table_name, column_name, **options)
  create_index = build_create_index_definition(table_name, column_name, **options)
  execute schema_creation.accept(create_index)
end

add_reference(table_name, ref_name, **options)

新增一個參考。預設上,參考欄位為 bigint,:type 選項可用於指定不同的類型。如果提供了 :polymorphic 選項,則會選擇性地新增一個 _type 欄位。

options hash 可包含下列金鑰

:type

參考欄位類型。預設為 :bigint

:index

加入適當的索引。預設為 true。請參閱 add_index 以了解此選項的用法。

:foreign_key

加入適當的外來鍵約束。預設為 false,傳遞 true 以加入。如果無法從關聯推斷出聯結表格,請傳遞 :to_table,並附上適當的表格名稱。

:polymorphic

是否應加入額外的 _type 欄位。預設為 false。

:null

欄位是否允許為空值。預設為 true。

建立一個沒有索引的 user_id bigint 欄位
add_reference(:products, :user, index: false)
建立一個 user_id 字串欄位
add_reference(:products, :user, type: :string)
建立 supplier_id、supplier_type 欄位
add_reference(:products, :supplier, polymorphic: true)
建立一個具有唯一索引的 supplier_id 欄位
add_reference(:products, :supplier, index: { unique: true })
建立一個具有命名索引的 supplier_id 欄位
add_reference(:products, :supplier, index: { name: "my_supplier_index" })
建立一個 supplier_id 欄位和適當的外來鍵
add_reference(:products, :supplier, foreign_key: true)
建立一個 supplier_id 欄位和指向 firms 表格的外來鍵
add_reference(:products, :supplier, foreign_key: { to_table: :firms })
別名為: add_belongs_to
# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 1030
def add_reference(table_name, ref_name, **options)
  ReferenceDefinition.new(ref_name, **options).add(table_name, self)
end

add_timestamps(table_name, **options)

將時間戳記(created_atupdated_at)欄位加入 table_name。其他選項(例如 :null)會轉發到 add_column

add_timestamps(:suppliers, null: true)
# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 1419
def add_timestamps(table_name, **options)
  fragments = add_timestamps_for_alter(table_name, **options)
  execute "ALTER TABLE #{quote_table_name(table_name)} #{fragments.join(', ')}"
end

assume_migrated_upto_version(version)

# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 1325
def assume_migrated_upto_version(version)
  version = version.to_i
  sm_table = quote_table_name(schema_migration.table_name)

  migrated = migration_context.get_all_versions
  versions = migration_context.migrations.map(&:version)

  unless migrated.include?(version)
    execute "INSERT INTO #{sm_table} (version) VALUES (#{quote(version)})"
  end

  inserting = (versions - migrated).select { |v| v < version }
  if inserting.any?
    if (duplicate = inserting.detect { |v| inserting.count(v) > 1 })
      raise "Duplicate migration #{duplicate}. Please renumber your migrations to resolve the conflict."
    end
    execute insert_versions_sql(inserting)
  end
end

build_create_table_definition(table_name, id: :primary_key, primary_key: nil, force: nil, **options)

傳回一個 TableDefinition 物件,其中包含有關表格的資訊,如果將相同的引數傳遞給 create_table,則會建立該表格。有關傳遞 table_name 和其他可傳遞的附加選項的資訊,請參閱 create_table

# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 328
def build_create_table_definition(table_name, id: :primary_key, primary_key: nil, force: nil, **options)
  table_definition = create_table_definition(table_name, **options.extract!(*valid_table_definition_options, :_skip_validate_options))
  table_definition.set_primary_key(table_name, id, primary_key, **options.extract!(*valid_primary_key_options, :_skip_validate_options))

  yield table_definition if block_given?

  table_definition
end

change_column(table_name, column_name, type, **options)

根據新的選項變更欄位的定義。有關可使用的選項的詳細資訊,請參閱 TableDefinition#column

change_column(:suppliers, :name, :string, limit: 80)
change_column(:accounts, :description, :text)
# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 695
def change_column(table_name, column_name, type, **options)
  raise NotImplementedError, "change_column is not implemented"
end

change_column_comment(table_name, column_name, comment_or_changes)

變更欄位的註解,或是在 nil 的情況下移除註解。

傳遞包含 :from:to 的雜湊值,將使此變更在遷移中可逆

change_column_comment(:posts, :state, from: "old_comment", to: "new_comment")
# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 1497
def change_column_comment(table_name, column_name, comment_or_changes)
  raise NotImplementedError, "#{self.class} does not support changing column comments"
end

change_column_default(table_name, column_name, default_or_changes)

設定欄位的新預設值

change_column_default(:suppliers, :qualification, 'new')
change_column_default(:accounts, :authorized, 1)

將預設值設定為 nil 會有效地刪除預設值

change_column_default(:users, :email, nil)

傳遞包含 :from:to 的雜湊值,將使此變更在遷移中可逆

change_column_default(:posts, :state, from: nil, to: "draft")
# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 713
def change_column_default(table_name, column_name, default_or_changes)
  raise NotImplementedError, "change_column_default is not implemented"
end

change_column_null(table_name, column_name, null, default = nil)

在欄位上設定或移除 NOT NULL 約束。null 旗標指出值是否可以為 NULL。例如

change_column_null(:users, :nickname, false)

表示暱稱不能為 NULL(新增約束),而

change_column_null(:users, :nickname, true)

允許它們為 NULL(刪除約束)。

此方法接受第四個選用引數,以將現有的 NULL 替換為其他值。在需要時啟用約束時使用該引數,因為否則那些列將會無效。

請注意,第四個參數不會設定欄位的預設值。

# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 742
def change_column_null(table_name, column_name, null, default = nil)
  raise NotImplementedError, "change_column_null is not implemented"
end

change_table(table_name, base = self, **options)

用於變更 table 中欄位的區塊。

# change_table() yields a Table instance
change_table(:suppliers) do |t|
  t.column :name, :string, limit: 60
  # Other column alterations here
end

options hash 可包含下列金鑰

:bulk

將此設定為 true 以使其成為大量變更查詢,例如

ALTER TABLE `users` ADD COLUMN age INT, ADD COLUMN birthdate DATETIME ...

預設為 false。

僅支援 MySQL 和 PostgreSQL 介面卡,其他地方會忽略。

新增欄位
change_table(:suppliers) do |t|
  t.column :name, :string, limit: 60
end
變更欄位類型
change_table(:suppliers) do |t|
  t.change :metadata, :json
end
新增 2 個整數欄位
change_table(:suppliers) do |t|
  t.integer :width, :height, null: false, default: 0
end
新增 created_at/updated_at 欄位
change_table(:suppliers) do |t|
  t.timestamps
end
新增外來鍵欄位
change_table(:suppliers) do |t|
  t.references :company
end

建立 company_id(bigint) 欄位。

新增多型外來鍵欄位
change_table(:suppliers) do |t|
  t.belongs_to :company, polymorphic: true
end

建立 company_type(varchar)company_id(bigint) 欄位。

移除欄位
change_table(:suppliers) do |t|
  t.remove :company
end
移除多個欄位
change_table(:suppliers) do |t|
  t.remove :company_id
  t.remove :width, :height
end
移除索引
change_table(:suppliers) do |t|
  t.remove_index :company_id
end

另請參閱 Table 以取得所有欄位轉換的詳細資料。

# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 496
def change_table(table_name, base = self, **options)
  if supports_bulk_alter? && options[:bulk]
    recorder = ActiveRecord::Migration::CommandRecorder.new(self)
    yield update_table_definition(table_name, recorder)
    bulk_change_table(table_name, recorder.commands)
  else
    yield update_table_definition(table_name, base)
  end
end

change_table_comment(table_name, comment_or_changes)

變更表格的註解,或在 nil 時移除。

傳遞包含 :from:to 的雜湊值,將使此變更在遷移中可逆

change_table_comment(:posts, from: "old_comment", to: "new_comment")
# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 1487
def change_table_comment(table_name, comment_or_changes)
  raise NotImplementedError, "#{self.class} does not support changing table comments"
end

check_constraint_exists?(table_name, **options)

檢查表格中是否存在檢查約束,針對給定的檢查約束定義。

check_constraint_exists?(:products, name: "price_check")
# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 1309
def check_constraint_exists?(table_name, **options)
  if !options.key?(:name) && !options.key?(:expression)
    raise ArgumentError, "At least one of :name or :expression must be supplied"
  end
  check_constraint_for(table_name, **options).present?
end

check_constraints(table_name)

傳回給定表格的檢查約束陣列。檢查約束表示為 CheckConstraintDefinition 物件。

# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 1240
def check_constraints(table_name)
  raise NotImplementedError
end

column_exists?(table_name, column_name, type = nil, **options)

檢查指定的資料表中是否存在欄位。

# Check a column exists
column_exists?(:suppliers, :name)

# Check a column exists of a particular type
#
# This works for standard non-casted types (eg. string) but is unreliable
# for types that may get cast to something else (eg. char, bigint).
column_exists?(:suppliers, :name, :string)

# Check a column exists with a specific definition
column_exists?(:suppliers, :name, :string, limit: 100)
column_exists?(:suppliers, :name, :string, default: 'default')
column_exists?(:suppliers, :name, :string, null: false)
column_exists?(:suppliers, :tax, :decimal, precision: 8, scale: 2)
# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 132
def column_exists?(table_name, column_name, type = nil, **options)
  column_name = column_name.to_s
  checks = []
  checks << lambda { |c| c.name == column_name }
  checks << lambda { |c| c.type == type.to_sym rescue nil } if type
  column_options_keys.each do |attr|
    checks << lambda { |c| c.send(attr) == options[attr] } if options.key?(attr)
  end

  columns(table_name).any? { |c| checks.all? { |check| check[c] } }
end

columns(table_name)

傳回 Column 物件陣列,其中包含 table_name 所指定的資料表。

# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 107
def columns(table_name)
  table_name = table_name.to_s
  definitions = column_definitions(table_name)
  definitions.map do |field|
    new_column_from_field(table_name, field, definitions)
  end
end

create_join_table(table_1, table_2, column_options: {}, **options)

使用前兩個引數的字典順序建立新的關聯資料表。這些引數可以是 字串符號

# Creates a table called 'assemblies_parts' with no id.
create_join_table(:assemblies, :parts)

您可以傳遞 options hash,其中可以包含下列金鑰

:table_name

設定資料表名稱,覆寫預設值。

:column_options

任何您想附加到欄位定義的額外選項。

:options

任何您想附加到資料表定義的額外選項。

:temporary

建立暫時資料表。

:force

設定為 true 以在建立資料表之前刪除資料表。預設為 false。

請注意,create_join_table 預設不會建立任何索引;您可以使用其區塊形式自行建立

create_join_table :products, :categories do |t|
  t.index :product_id
  t.index :category_id
end
將後端特定選項新增到產生的 SQL (MySQL)
create_join_table(:assemblies, :parts, options: 'ENGINE=InnoDB DEFAULT CHARSET=utf8')

產生

CREATE TABLE assemblies_parts (
  assembly_id bigint NOT NULL,
  part_id bigint NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8
# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 375
def create_join_table(table_1, table_2, column_options: {}, **options)
  join_table_name = find_join_table_name(table_1, table_2, options)

  column_options.reverse_merge!(null: false, index: false)

  t1_ref, t2_ref = [table_1, table_2].map { |t| reference_name_for_table(t) }

  create_table(join_table_name, **options.merge!(id: false)) do |td|
    td.references t1_ref, **column_options
    td.references t2_ref, **column_options
    yield td if block_given?
  end
end

create_table(table_name, id: :primary_key, primary_key: nil, force: nil, **options, &block)

建立新的資料表,名稱為 table_nametable_name 可以是 字串符號

使用 create_table 的方法有兩種。您可以使用區塊形式或一般形式,如下所示

區塊形式

# create_table() passes a TableDefinition object to the block.
# This form will not only create the table, but also columns for the
# table.

create_table(:suppliers) do |t|
  t.column :name, :string, limit: 60
  # Other fields here
end

區塊形式,使用簡寫

# You can also use the column types as method calls, rather than calling the column method.
create_table(:suppliers) do |t|
  t.string :name, limit: 60
  # Other fields here
end

一般形式

# Creates a table called 'suppliers' with no columns.
create_table(:suppliers)
# Add a column to 'suppliers'.
add_column(:suppliers, :name, :string, {limit: 60})

options hash 可包含下列金鑰

:id

是否自動新增主鍵欄位。預設為 true。ActiveRecord::Base.has_and_belongs_to_many 的關聯資料表應將其設定為 false。

可以使用 Symbol 來指定所產生主鍵欄位的類型。

:primary_key

主鍵的名稱,如果要自動新增的話。預設為 id。如果 :id 為 false,則會略過此選項。

如果傳遞陣列,則會建立複合主鍵。

請注意,Active Record 模型會自動偵測其主鍵。可以在模型上使用 self.primary_key= 來避免這種情況,以明確定義鍵。

:options

任何您想附加到資料表定義的額外選項。

:temporary

建立暫時資料表。

:force

設為 true 以在建立表格之前將其刪除。設為 :cascade 以同時刪除依賴物件。預設為 false。

:if_not_exists

設為 true 以避免在表格已存在時引發錯誤。預設為 false。

:as

用於產生表格的 SQL。使用此選項時,會略過區塊,以及 :id:primary_key 選項。

將後端特定選項新增到產生的 SQL (MySQL)
create_table(:suppliers, options: 'ENGINE=InnoDB DEFAULT CHARSET=utf8mb4')

產生

CREATE TABLE suppliers (
  id bigint auto_increment PRIMARY KEY
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
重新命名主鍵欄位
create_table(:objects, primary_key: 'guid') do |t|
  t.column :name, :string, limit: 80
end

產生

CREATE TABLE objects (
  guid bigint auto_increment PRIMARY KEY,
  name varchar(80)
)
變更主鍵欄位類型
create_table(:tags, id: :string) do |t|
  t.column :label, :string
end

產生

CREATE TABLE tags (
  id varchar PRIMARY KEY,
  label varchar
)
建立複合主鍵
create_table(:orders, primary_key: [:product_id, :client_id]) do |t|
  t.belongs_to :product
  t.belongs_to :client
end

產生

CREATE TABLE orders (
    product_id bigint NOT NULL,
    client_id bigint NOT NULL
);

ALTER TABLE ONLY "orders"
  ADD CONSTRAINT orders_pkey PRIMARY KEY (product_id, client_id);
不要新增主鍵欄位
create_table(:categories_suppliers, id: false) do |t|
  t.column :category_id, :bigint
  t.column :supplier_id, :bigint
end

產生

CREATE TABLE categories_suppliers (
  category_id bigint,
  supplier_id bigint
)
根據查詢建立暫時表格
create_table(:long_query, temporary: true,
  as: "SELECT * FROM orders INNER JOIN line_items ON order_id=orders.id")

產生

CREATE TEMPORARY TABLE long_query AS
  SELECT * FROM orders INNER JOIN line_items ON order_id=orders.id

另請參閱 TableDefinition#column,以取得如何建立欄位的詳細資料。

# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 293
def create_table(table_name, id: :primary_key, primary_key: nil, force: nil, **options, &block)
  validate_create_table_options!(options)
  validate_table_length!(table_name) unless options[:_uses_legacy_table_name]
  td = build_create_table_definition(table_name, id: id, primary_key: primary_key, force: force, **options, &block)

  if force
    drop_table(table_name, force: force, if_exists: true)
  else
    schema_cache.clear_data_source_cache!(table_name.to_s)
  end

  result = execute schema_creation.accept(td)

  unless supports_indexes_in_create?
    td.indexes.each do |column_name, index_options|
      add_index(table_name, column_name, **index_options, if_not_exists: td.if_not_exists)
    end
  end

  if supports_comments? && !supports_comments_in_create?
    if table_comment = td.comment.presence
      change_table_comment(table_name, table_comment)
    end

    td.columns.each do |column|
      change_column_comment(table_name, column.name, column.comment) if column.comment.present?
    end
  end

  result
end

data_source_exists?(name)

檢查資料來源 name 是否存在於資料庫中。

data_source_exists?(:ebooks)
# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 44
def data_source_exists?(name)
  query_values(data_source_sql(name), "SCHEMA").any? if name.present?
rescue NotImplementedError
  data_sources.include?(name.to_s)
end

data_sources()

傳回可用於支援 Active Record 模型的關聯名稱。對大多數的配接器而言,這表示所有 tablesviews

# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 34
def data_sources
  query_values(data_source_sql, "SCHEMA")
rescue NotImplementedError
  tables | views
end

drop_join_table(table_1, table_2, **options)

刪除由給定引數指定的關聯表格。有關詳細資訊,請參閱 create_join_tabledrop_table

雖然此指令會忽略區塊(如果提供),但提供一個區塊在遷移的 change 方法中可能會有幫助,以便可以還原。在這種情況下,區塊將由 create_join_table 使用。

# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 413
def drop_join_table(table_1, table_2, **options)
  join_table_name = find_join_table_name(table_1, table_2, options)
  drop_table(join_table_name, **options)
end

drop_table(table_name, **options)

從資料庫中刪除表格。

:force

設定為 :cascade 以同時刪除依賴物件。預設為 false。

:if_exists

設定為 true 以僅在表格存在時刪除表格。預設為 false。

雖然此指令會忽略大多數 options 和區塊(如果提供),但提供這些內容在遷移的 change 方法中可能會有幫助,以便可以還原。在這種情況下,options 和區塊將由 create_table 使用。

# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 526
def drop_table(table_name, **options)
  schema_cache.clear_data_source_cache!(table_name.to_s)
  execute "DROP TABLE#{' IF EXISTS' if options[:if_exists]} #{quote_table_name(table_name)}"
end

foreign_key_exists?(from_table, to_table = nil, **options)

檢查表格上是否存在給定外來金鑰定義的外來金鑰。

# Checks to see if a foreign key exists.
foreign_key_exists?(:accounts, :branches)

# Checks to see if a foreign key on a specified column exists.
foreign_key_exists?(:accounts, column: :owner_id)

# Checks to see if a foreign key with a custom name exists.
foreign_key_exists?(:accounts, name: "special_fk_name")
# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 1204
def foreign_key_exists?(from_table, to_table = nil, **options)
  foreign_key_for(from_table, to_table: to_table, **options).present?
end

foreign_keys(table_name)

傳回給定表格的外來金鑰陣列。外來金鑰表示為 ForeignKeyDefinition 物件。

# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 1070
def foreign_keys(table_name)
  raise NotImplementedError, "foreign_keys is not implemented"
end

index_exists?(table_name, column_name, **options)

檢查表格中是否存在特定索引定義的索引。

# Check an index exists
index_exists?(:suppliers, :company_id)

# Check an index on multiple columns exists
index_exists?(:suppliers, [:company_id, :company_type])

# Check a unique index exists
index_exists?(:suppliers, :company_id, unique: true)

# Check an index with a custom name exists
index_exists?(:suppliers, :company_id, name: "idx_company_id")

# Check a valid index exists (PostgreSQL only)
index_exists?(:suppliers, :company_id, valid: true)
# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 102
def index_exists?(table_name, column_name, **options)
  indexes(table_name).any? { |i| i.defined_for?(column_name, **options) }
end

index_name_exists?(table_name, index_name)

驗證具有特定名稱的索引是否存在。

# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 978
def index_name_exists?(table_name, index_name)
  index_name = index_name.to_s
  indexes(table_name).detect { |i| i.name == index_name }
end

indexes(table_name)

傳回指定表格的索引陣列。

# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 81
def indexes(table_name)
  raise NotImplementedError, "#indexes is not implemented"
end

max_index_name_size()

傳回索引名稱的最大長度(以位元組為單位)。

# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 1553
def max_index_name_size
  62
end

native_database_types()

傳回抽象資料類型對應至原生資料庫類型的雜湊。有關已識別的抽象資料類型的詳細資訊,請參閱 TableDefinition#column

# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 14
def native_database_types
  {}
end

options_include_default?(options)

# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 1477
def options_include_default?(options)
  options.include?(:default) && !(options[:null] == false && options[:default].nil?)
end

primary_key(table_name)

僅傳回表格的主鍵

# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 145
def primary_key(table_name)
  pk = primary_keys(table_name)
  pk = pk.first unless pk.size > 1
  pk
end

remove_belongs_to(table_name, ref_name, foreign_key: false, polymorphic: false, **options)

別名為:remove_reference

remove_check_constraint(table_name, expression = nil, if_exists: false, **options)

從資料表中移除指定的檢查約束。移除不存在的檢查約束會引發錯誤。

remove_check_constraint :products, name: "price_check"

若要忽略不存在的檢查約束,而不是引發錯誤,請使用 if_exists 選項。

remove_check_constraint :products, name: "price_check", if_exists: true

如果存在,expression 參數將會被忽略。在遷移的 change 方法中提供此參數會很有幫助,以便可以還原。在這種情況下,expression 將會由 add_check_constraint 使用。

# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 1291
def remove_check_constraint(table_name, expression = nil, if_exists: false, **options)
  return unless supports_check_constraints?

  return if if_exists && !check_constraint_exists?(table_name, **options)

  chk_name_to_delete = check_constraint_for!(table_name, expression: expression, **options).name

  at = create_alter_table(table_name)
  at.drop_check_constraint(chk_name_to_delete)

  execute schema_creation.accept(at)
end

remove_column(table_name, column_name, type = nil, **options)

從資料表定義中移除欄位。

remove_column(:suppliers, :qualification)

如果存在,typeoptions 參數將會被忽略。在遷移的 change 方法中提供這些參數會很有幫助,以便可以還原。在這種情況下,typeoptions 將會由 add_column 使用。根據您使用的資料庫,使用此欄位的索引可能會自動移除或修改,以從索引中移除此欄位。

如果提供的選項包含 if_exists 鍵,它將用於檢查欄位是否不存在。如果欄位已經使用,這將會靜默地忽略遷移,而不是引發錯誤。

remove_column(:suppliers, :qualification, if_exists: true)
# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 683
def remove_column(table_name, column_name, type = nil, **options)
  return if options[:if_exists] == true && !column_exists?(table_name, column_name)

  execute "ALTER TABLE #{quote_table_name(table_name)} #{remove_column_for_alter(table_name, column_name, type, **options)}"
end

remove_columns(table_name, *column_names, type: nil, **options)

移除表格定義中指定的欄位。

remove_columns(:suppliers, :qualification, :experience)

type 和其他欄位選項可以傳遞,以使遷移可逆。

remove_columns(:suppliers, :qualification, :experience, type: :string, null: false)
# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 659
def remove_columns(table_name, *column_names, type: nil, **options)
  if column_names.empty?
    raise ArgumentError.new("You must specify at least one column name. Example: remove_columns(:people, :first_name)")
  end

  remove_column_fragments = remove_columns_for_alter(table_name, *column_names, type: type, **options)
  execute "ALTER TABLE #{quote_table_name(table_name)} #{remove_column_fragments.join(', ')}"
end

remove_foreign_key(from_table, to_table = nil, **options)

從表格中移除指定的外部金鑰。在遷移回滾的情況下,將使用提供的任何選項參數重新加入外部金鑰。建議您提供建立外部金鑰時使用的任何選項,以便可以正確地還原遷移。

移除 accounts.branch_id 上的外部金鑰。

remove_foreign_key :accounts, :branches

移除 accounts.owner_id 上的外部金鑰。

remove_foreign_key :accounts, column: :owner_id

移除 accounts.owner_id 上的外部金鑰。

remove_foreign_key :accounts, to_table: :owners

移除 accounts 表格上名為 special_fk_name 的外部金鑰。

remove_foreign_key :accounts, name: :special_fk_name

在嘗試移除外部金鑰之前檢查其是否存在。將會靜默地忽略不存在的索引。

remove_foreign_key :accounts, :branches, if_exists: true

options hash 接受與 SchemaStatements#add_foreign_key 相同的鍵,並新增

:to_table

包含參考主鍵的表格名稱。

# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 1181
def remove_foreign_key(from_table, to_table = nil, **options)
  return unless use_foreign_keys?
  return if options.delete(:if_exists) == true && !foreign_key_exists?(from_table, to_table)

  fk_name_to_delete = foreign_key_for!(from_table, to_table: to_table, **options).name

  at = create_alter_table from_table
  at.drop_foreign_key fk_name_to_delete

  execute schema_creation.accept(at)
end

remove_index(table_name, column_name = nil, **options)

從表格中移除指定的索引。

如果 accounts 表格中只有一個這樣的索引,則移除 branch_id 上的索引。

remove_index :accounts, :branch_id

如果 accounts 表格中只有一個這樣的索引,則移除 branch_id 上的索引。

remove_index :accounts, column: :branch_id

如果 accounts 表格中只有一個這樣的索引,則移除 branch_idparty_id 上的索引。

remove_index :accounts, column: [:branch_id, :party_id]

移除 accounts 表格中名為 by_branch_party 的索引。

remove_index :accounts, name: :by_branch_party

移除 accounts 表格中名為 by_branch_partybranch_id 上的索引。

remove_index :accounts, :branch_id, name: :by_branch_party

在嘗試移除索引之前檢查其是否存在。將會靜默地忽略不存在的索引。

remove_index :accounts, if_exists: true

移除 accounts 表格中名為 by_branch_party 的索引 concurrently

remove_index :accounts, name: :by_branch_party, algorithm: :concurrently

注意:僅 PostgreSQL 支援。

在交易中不支援同時移除索引。

如需更多資訊,請參閱 「交易遷移」區段

# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 937
def remove_index(table_name, column_name = nil, **options)
  return if options[:if_exists] && !index_exists?(table_name, column_name, **options)

  index_name = index_name_for_remove(table_name, column_name, options)

  execute "DROP INDEX #{quote_column_name(index_name)} ON #{quote_table_name(table_name)}"
end

remove_reference(table_name, ref_name, foreign_key: false, polymorphic: false, **options)

移除參考。如果存在,也會移除 type 欄位。

移除參考
remove_reference(:products, :user, index: false)
移除多型態參考
remove_reference(:products, :supplier, polymorphic: true)
移除具有外來鍵的參考
remove_reference(:products, :user, foreign_key: true)
# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 1049
def remove_reference(table_name, ref_name, foreign_key: false, polymorphic: false, **options)
  conditional_options = options.slice(:if_exists, :if_not_exists)

  if foreign_key
    reference_name = Base.pluralize_table_names ? ref_name.to_s.pluralize : ref_name
    if foreign_key.is_a?(Hash)
      foreign_key_options = foreign_key.merge(conditional_options)
    else
      foreign_key_options = { to_table: reference_name, **conditional_options }
    end
    foreign_key_options[:column] ||= "#{ref_name}_id"
    remove_foreign_key(table_name, **foreign_key_options)
  end

  remove_column(table_name, "#{ref_name}_id", **conditional_options)
  remove_column(table_name, "#{ref_name}_type", **conditional_options) if polymorphic
end

remove_timestamps(table_name, **options)

從資料表定義中移除時間戳記欄位(created_atupdated_at)。

remove_timestamps(:suppliers)
# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 1428
def remove_timestamps(table_name, **options)
  remove_columns table_name, :updated_at, :created_at
end

rename_column(table_name, column_name, new_column_name)

重新命名欄位。

rename_column(:suppliers, :description, :name)
# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 750
def rename_column(table_name, column_name, new_column_name)
  raise NotImplementedError, "rename_column is not implemented"
end

rename_index(table_name, old_name, new_name)

重新命名索引。

index_people_on_last_name 索引重新命名為 index_users_on_last_name

rename_index :people, 'index_people_on_last_name', 'index_users_on_last_name'
# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 951
def rename_index(table_name, old_name, new_name)
  old_name = old_name.to_s
  new_name = new_name.to_s
  validate_index_length!(table_name, new_name)

  # this is a naive implementation; some DBs may support this more efficiently (PostgreSQL, for instance)
  old_index_def = indexes(table_name).detect { |i| i.name == old_name }
  return unless old_index_def
  add_index(table_name, old_index_def.columns, name: new_name, unique: old_index_def.unique)
  remove_index(table_name, name: old_name)
end

rename_table(table_name, new_name, **)

重新命名資料表。

rename_table('octopuses', 'octopi')
# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 510
def rename_table(table_name, new_name, **)
  raise NotImplementedError, "rename_table is not implemented"
end

table_alias_for(table_name)

根據目前轉接器的限制,截斷資料表別名。

# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 28
def table_alias_for(table_name)
  table_name[0...table_alias_length].tr(".", "_")
end

table_comment(table_name)

傳回儲存在資料庫中繼資料中的表格註解。

# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 23
def table_comment(table_name)
  nil
end

table_exists?(table_name)

檢查資料庫中是否存在表格 table_name

table_exists?(:developers)
# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 59
def table_exists?(table_name)
  query_values(data_source_sql(table_name, type: "BASE TABLE"), "SCHEMA").any? if table_name.present?
rescue NotImplementedError
  tables.include?(table_name.to_s)
end

table_options(table_name)

# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 18
def table_options(table_name)
  nil
end

tables()

傳回資料庫中定義的表格名稱陣列。

# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 51
def tables
  query_values(data_source_sql(type: "BASE TABLE"), "SCHEMA")
end

use_foreign_keys?()

# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 1505
def use_foreign_keys?
  supports_foreign_keys? && foreign_keys_enabled?
end

view_exists?(view_name)

檢查資料庫中是否存在檢視 view_name

view_exists?(:ebooks)
# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 74
def view_exists?(view_name)
  query_values(data_source_sql(view_name, type: "VIEW"), "SCHEMA").any? if view_name.present?
rescue NotImplementedError
  views.include?(view_name.to_s)
end

views()

傳回資料庫中定義的檢視名稱陣列。

# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 66
def views
  query_values(data_source_sql(type: "VIEW"), "SCHEMA")
end