跳至內容 跳至搜尋
方法
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_reference

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

新增一個新的檢查約束到表格。expression 是一個 String 類型的可驗證布林條件表示式。

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

產生

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

options 雜湊可以包含以下鍵值

:name

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

:if_not_exists

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

:validate

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

# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 1293
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)

table_name 新增一個新的類型為 type,名稱為 column_name 的欄位。

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

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

您可以使用不在此清單中的類型,只要您的資料庫支援它(例如,MySQL 中的「polygon」),但这將不是資料庫不可知的,通常應避免使用。

可用的選項有(預設情況下不存在):

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

  • :collation - 指定 :string:text 欄位的排序規則。如果未指定,則欄位將與表格具有相同的排序規則。

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

  • :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 636
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 是一個 10 個字元長的字串,它是由 from_tablecolumn 確定性地生成的。可以使用 :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 雜湊可以包含以下鍵值

: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 1173
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 可以是單個 SymbolArray 類型的符號。

索引將根據表格和欄位名稱命名,除非您傳遞 :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 支援。

建立 NULL 值被視為相等的索引
add_index(:people, :last_name, nulls_not_distinct: true)

產生

CREATE INDEX index_people_on_last_name ON people (last_name) NULLS NOT DISTINCT

注意:僅 PostgreSQL 15.0.0 及更高版本支援。

使用特定方法建立索引
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

add_index(:developers, :name, algorithm: :inplace)
# CREATE INDEX `index_developers_on_name` ON `developers` (`name`) ALGORITHM = INPLACE -- MySQL

注意:僅 PostgreSQL 和 MySQL 支援。

交易中不支援同步新增索引。

有關更多資訊,請參閱「交易遷移」章節

# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 915
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 雜湊可以包含以下鍵值

:type

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

:index

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

:foreign_key

新增適當的外鍵約束。預設為 false,傳遞 true 表示新增。如果無法從關聯推斷聯結表格,請使用適當的表格名稱傳遞 :to_table

:polymorphic

是否應新增額外的 _type 欄位。預設為 false。

:null

欄位是否允許 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 1063
def add_reference(table_name, ref_name, **options)
  ReferenceDefinition.new(ref_name, **options).add(table_name, self)
end

add_timestamps(table_name, **options)

在 `table_name` 中加入時間戳記欄位(`created_at` 和 `updated_at`)。其他選項(例如 `:null`)會被轉送到 `add_column`

add_timestamps(:suppliers, null: true)
# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 1459
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 1364
def assume_migrated_upto_version(version)
  version = version.to_i
  sm_table = quote_table_name(pool.schema_migration.table_name)

  migration_context = pool.migration_context
  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 333
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 711
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 1537
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 729
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 758
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 雜湊可以包含以下鍵值

: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 510
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 1527
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 1341
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 1273
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)

傳回由 `table_name` 指定的表格的 `Column` 物件陣列。

# 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)

使用前兩個參數的字典順序建立名稱,建立新的連接表格。這些參數可以是 `String``Symbol`

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

# Creates a table called 'paper_boxes_papers' with no id.
create_join_table('papers', 'paper_boxes')

重複的前綴會合併成單一前綴。這對於命名空間模型(例如 Music::Artist 和 Music::Record)很有用。

# Creates a table called 'music_artists_records' with no id.
create_join_table('music_artists', 'music_records')

您可以傳遞一個 `options` 雜湊,其中可以包含以下鍵:

: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 389
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_name` 建立新表格。 `table_name` 可以是 `String``Symbol`

有兩種方法可以使用 `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 雜湊可以包含以下鍵值

: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]

  if force && options.key?(:if_not_exists)
    raise ArgumentError, "Options `:force` and `:if_not_exists` cannot be used simultaneously."
  end

  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 模型的關聯名稱。對於大多數適配器來說,這意味著所有 表格視圖

# 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 427
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_names, **options)

從資料庫中刪除一個或多個表格。

:force

設定為 :cascade 也會刪除相依物件。預設值為 false。

:if_exists

設定為 true 則僅在表格存在時才刪除它。預設值為 false。

雖然此命令會忽略大多數 options 和區塊(如果有的話),但在遷移的 change 方法中提供這些可能會有幫助,以便它可以被還原。在這種情況下,options 和區塊將由 create_table 使用,除非您提供多個表格,這是scholarship 不支援的。

# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 540
def drop_table(*table_names, **options)
  table_names.each do |table_name|
    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
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 1237
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 1103
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 1011
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 1593
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 1517
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_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 1324
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 699
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 675
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 雜湊接受與 SchemaStatements#add_foreign_key 相同的鍵,並新增

:to_table

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

# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 1214
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 表格中 branch_id 上名為 by_branch_party 的索引。

remove_index :accounts, :branch_id, name: :by_branch_party

在嘗試移除索引之前,檢查它是否存在。將會略過不存在的索引。

remove_index :accounts, if_exists: true

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

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

注意:僅 PostgreSQL 支援。

交易中不支援並行移除索引。

有關更多資訊,請參閱「交易遷移」章節

# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 966
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)
也稱為:remove_belongs_to
# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 1082
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 1468
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 766
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 980
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 524
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 1545
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