跳至內容 跳至搜尋

Active Record 連線配接器 表格

以抽象的方式表示 SQL 表格,用於更新表格。另請參閱 TableDefinitionconnection.create_table

可用的轉換為

change_table :table do |t|
  t.primary_key
  t.column
  t.index
  t.rename_index
  t.timestamps
  t.change
  t.change_default
  t.change_null
  t.rename
  t.references
  t.belongs_to
  t.check_constraint
  t.string
  t.text
  t.integer
  t.bigint
  t.float
  t.decimal
  t.numeric
  t.datetime
  t.timestamp
  t.time
  t.date
  t.binary
  t.blob
  t.boolean
  t.foreign_key
  t.json
  t.virtual
  t.remove
  t.remove_foreign_key
  t.remove_references
  t.remove_belongs_to
  t.remove_index
  t.remove_check_constraint
  t.remove_timestamps
end
方法
B
C
F
I
N
R
T
已包含模組

屬性

[R] name

類別公開方法

new(table_name, base)

# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 716
def initialize(table_name, base)
  @name = table_name
  @base = base
end

實例公開方法

belongs_to(*args, **options)

references 的別名

change(column_name, type, **options)

根據新的選項更改欄位的定義。

t.change(:name, :string, limit: 80)
t.change(:description, :text)

有關您可以使用的選項的詳細資訊,請參閱 TableDefinition#column

# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 793
def change(column_name, type, **options)
  raise_on_if_exist_options(options)
  @base.change_column(name, column_name, type, **options)
end

change_default(column_name, default_or_changes)

設定欄位的新預設值。

t.change_default(:qualification, 'new')
t.change_default(:authorized, 1)
t.change_default(:status, from: nil, to: "draft")

請參閱 connection.change_column_default

# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 805
def change_default(column_name, default_or_changes)
  @base.change_column_default(name, column_name, default_or_changes)
end

change_null(column_name, null, default = nil)

設定或移除欄位上的 NOT NULL 條件約束。

t.change_null(:qualification, true)
t.change_null(:qualification, false, 0)

請參閱 connection.change_column_null

# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 815
def change_null(column_name, null, default = nil)
  @base.change_column_null(name, column_name, null, default)
end

check_constraint(*args, **options)

新增檢查條件約束。

t.check_constraint("price > 0", name: "price_check")

請參閱 connection.add_check_constraint

# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 925
def check_constraint(*args, **options)
  @base.add_check_constraint(name, *args, **options)
end

check_constraint_exists?(*args, **options)

檢查表格上是否存在 check_constraint

unless t.check_constraint_exists?(name: "price_check")
  t.check_constraint("price > 0", name: "price_check")
end

請參閱 connection.check_constraint_exists?

# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 945
def check_constraint_exists?(*args, **options)
  @base.check_constraint_exists?(name, *args, **options)
end

column(column_name, type, index: nil, **options)

將新欄位新增至指定的表格。

t.column(:name, :string)

有關您可以使用的選項的詳細資訊,請參閱 TableDefinition#column

# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 726
def column(column_name, type, index: nil, **options)
  raise_on_if_exist_options(options)
  @base.add_column(name, column_name, type, **options)
  if index
    index_options = index.is_a?(Hash) ? index : {}
    index(column_name, **index_options)
  end
end

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

檢查欄位是否存在。

t.string(:name) unless t.column_exists?(:name, :string)

請參閱 connection.column_exists?

# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 740
def column_exists?(column_name, type = nil, **options)
  @base.column_exists?(name, column_name, type, **options)
end

foreign_key(*args, **options)

使用提供的表格名稱將外部鍵新增至表格。

t.foreign_key(:authors)
t.foreign_key(:authors, column: :author_id, primary_key: "id")

請參閱 connection.add_foreign_key

# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 895
def foreign_key(*args, **options)
  raise_on_if_exist_options(options)
  @base.add_foreign_key(name, *args, **options)
end

foreign_key_exists?(*args, **options)

檢查外部鍵是否存在。

t.foreign_key(:authors) unless t.foreign_key_exists?(:authors)

請參閱 connection.foreign_key_exists?

# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 916
def foreign_key_exists?(*args, **options)
  @base.foreign_key_exists?(name, *args, **options)
end

index(column_name, **options)

將新索引新增至表格。 column_name 可以是單個 SymbolArray 的 Symbols。

t.index(:name)
t.index([:branch_id, :party_id], unique: true)
t.index([:branch_id, :party_id], unique: true, name: 'by_branch_party')

有關您可以使用的選項的詳細資訊,請參閱 connection.add_index

# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 752
def index(column_name, **options)
  raise_on_if_exist_options(options)
  @base.add_index(name, column_name, **options)
end

index_exists?(column_name, **options)

檢查索引是否存在。

unless t.index_exists?(:branch_id)
  t.index(:branch_id)
end

請參閱 connection.index_exists?

# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 764
def index_exists?(column_name, **options)
  @base.index_exists?(name, column_name, **options)
end

references(*args, **options)

新增參考。

t.references(:user)
t.belongs_to(:supplier, foreign_key: true)

有關您可以使用的選項的詳細資訊,請參閱 connection.add_reference

另有別名:belongs_to
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 867
def references(*args, **options)
  raise_on_if_exist_options(options)
  args.each do |ref_name|
    @base.add_reference(name, ref_name, **options)
  end
end

remove(*column_names, **options)

從表格定義中移除欄位。

t.remove(:qualification)
t.remove(:qualification, :experience)

請參閱 connection.remove_columns

# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 825
def remove(*column_names, **options)
  raise_on_if_exist_options(options)
  @base.remove_columns(name, *column_names, **options)
end

remove_belongs_to(*args, **options)

remove_check_constraint(*args, **options)

從表格中移除指定的檢查條件約束。

t.remove_check_constraint(name: "price_check")

請參閱 connection.remove_check_constraint

# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 934
def remove_check_constraint(*args, **options)
  @base.remove_check_constraint(name, *args, **options)
end

remove_foreign_key(*args, **options)

從表格中移除指定的外部鍵。

t.remove_foreign_key(:authors)
t.remove_foreign_key(column: :author_id)

請參閱 connection.remove_foreign_key

# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 906
def remove_foreign_key(*args, **options)
  raise_on_if_exist_options(options)
  @base.remove_foreign_key(name, *args, **options)
end

remove_index(column_name = nil, **options)

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

t.remove_index(:branch_id)
t.remove_index(column: [:branch_id, :party_id])
t.remove_index(name: :by_branch_party)
t.remove_index(:branch_id, name: :by_branch_party)

請參閱 connection.remove_index

# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 838
def remove_index(column_name = nil, **options)
  raise_on_if_exist_options(options)
  @base.remove_index(name, column_name, **options)
end

remove_references(*args, **options)

移除參考。選擇性地移除 type 欄位。

t.remove_references(:user)
t.remove_belongs_to(:supplier, polymorphic: true)

請參閱 connection.remove_reference

另有別名:remove_belongs_to
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 881
def remove_references(*args, **options)
  raise_on_if_exist_options(options)
  args.each do |ref_name|
    @base.remove_reference(name, ref_name, **options)
  end
end

remove_timestamps(**options)

從表格中移除時間戳記欄位(created_atupdated_at)。

t.remove_timestamps

請參閱 connection.remove_timestamps

# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 848
def remove_timestamps(**options)
  @base.remove_timestamps(name, **options)
end

rename(column_name, new_column_name)

重新命名欄位。

t.rename(:description, :name)

請參閱 connection.rename_column

# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 857
def rename(column_name, new_column_name)
  @base.rename_column(name, column_name, new_column_name)
end

rename_index(index_name, new_index_name)

重新命名表格上指定的索引。

t.rename_index(:user_id, :account_id)

請參閱 connection.rename_index

# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 773
def rename_index(index_name, new_index_name)
  @base.rename_index(name, index_name, new_index_name)
end

timestamps(**options)

新增時間戳記(created_atupdated_at)欄位到表格。

t.timestamps(null: false)

請參閱 connection.add_timestamps

# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 782
def timestamps(**options)
  raise_on_if_exist_options(options)
  @base.add_timestamps(name, **options)
end