跳至内容 跳至搜索

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 可以是单个 Symbol,或者是一个 Array 的 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