Active Record 连接适配器 表¶ ↑
以抽象方式表示 SQL 表,用于更新表。另请参阅 TableDefinition 和 connection.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
Attributes
| [R] | name |
类公共方法
new(table_name, base) 链接
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 712 def initialize(table_name, base) @name = table_name @base = base end
实例公共方法
change(column_name, type, **options) 链接
根据新选项更改列的定义。
t.change(:name, :string, limit: 80) t.change(:description, :text)
有关可用的选项,请参阅 TableDefinition#column。
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 789 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")
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 801 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)
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 811 def change_null(column_name, null, default = nil) @base.change_column_null(name, column_name, null, default) end
check_constraint(*args, **options) 链接
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 921 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
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 941 def check_constraint_exists?(*args, **options) @base.check_constraint_exists?(name, *args, **options) end
column(column_name, type, index: nil, **options) 链接
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 722 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) 链接
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 736 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")
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 891 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)
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 912 def foreign_key_exists?(*args, **options) @base.foreign_key_exists?(name, *args, **options) end
index(column_name, **options) 链接
向表添加新索引。column_name 可以是单个 Symbol,也可以是 Symbols 的 Array。
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。
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 748 def index(column_name, **options) raise_on_if_exist_options(options) @base.add_index(name, column_name, **options) end
index_exists?(column_name = nil, **options) 链接
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 760 def index_exists?(column_name = nil, **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。
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 863 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)
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 821 def remove(*column_names, **options) raise_on_if_exist_options(options) @base.remove_columns(name, *column_names, **options) end
remove_check_constraint(*args, **options) 链接
从表中删除指定的检查约束。
t.remove_check_constraint(name: "price_check")
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 930 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)
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 902 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)
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 834 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)
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 877 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) 链接
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 844 def remove_timestamps(**options) @base.remove_timestamps(name, **options) end
rename(column_name, new_column_name) 链接
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 853 def rename(column_name, new_column_name) @base.rename_column(name, column_name, new_column_name) end
rename_index(index_name, new_index_name) 链接
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 769 def rename_index(index_name, new_index_name) @base.rename_index(name, index_name, new_index_name) end
timestamps(**options) 链接
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 778 def timestamps(**options) raise_on_if_exist_options(options) @base.add_timestamps(name, **options) end