Spec-Zone.ru › Ruby on Rails 7.2

class ActiveRecord::ConnectionAdapters::Table

Parent:
Объект
Included modules:
ActiveRecord::ConnectionAdapters::ColumnMethods

Active Record Connection Adapters Table

Представляет 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

Атрибуты

name[R]

Открытые методы класса

new(table_name, base) Показать исходный код
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 711
def initialize(table_name, base)
  @name = table_name
  @base = base
end

Публичные методы экземпляра

belongs_to(*args, **options)
Alias for: references
change(column_name, type, **options) Show source
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 788
def change(column_name, type, **options)
  raise_on_if_exist_options(options)
  @base.change_column(name, column_name, type, **options)
end

Изменяет определение столбца в соответствии с новыми параметрами.

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

См. TableDefinition#column для подробного описания параметров, которые можно использовать.

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

Устанавливает новое значение по умолчанию для столбца.

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

См. connection.change_column_default

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

Устанавливает или удаляет ограничение NOT NULL для столбца.

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

См. connection.change_column_null

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

Добавляет ограничение проверки.

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

См. connection.add_check_constraint

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

Проверяет, существует ли check_constraint в таблице.

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

См. connection.check_constraint_exists?

column(column_name, type, index: nil, **options) Show source
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 721
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

Добавляет новый столбец в указанную таблицу.

t.column(:name, :string)

См. TableDefinition#column для подробного описания параметров, которые можно использовать.

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

Проверяет, существует ли столбец.

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

См. connection.column_exists?

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

Добавляет внешний ключ в таблицу, используя заданное имя таблицы.

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

См. connection.add_foreign_key

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

Проверяет, существует ли внешний ключ.

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

См. connection.foreign_key_exists?

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

Добавляет новый индекс в таблицу. column_name может быть одиночным Symbol, или 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 для подробного описания параметров, которые можно использовать.

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

Проверяет, существует ли индекс.

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

См. connection.index_exists?

references(*args, **options) Show source
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 862
def references(*args, **options)
  raise_on_if_exist_options(options)
  args.each do |ref_name|
    @base.add_reference(name, ref_name, **options)
  end
end

Добавляет ссылку.

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

См. connection.add_reference для подробного описания параметров, которые можно использовать.

Также является псевдонимом для: belongs_to
remove(*column_names, **options) Show source
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 820
def remove(*column_names, **options)
  raise_on_if_exist_options(options)
  @base.remove_columns(name, *column_names, **options)
end

Удаляет столбец (столбцы) из определения таблицы.

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

См. connection.remove_columns

remove_belongs_to(*args, **options)
Alias for: remove_references
remove_check_constraint(*args, **options) Show source
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 929
def remove_check_constraint(*args, **options)
  @base.remove_check_constraint(name, *args, **options)
end

Удаляет указанное ограничение проверки из таблицы.

t.remove_check_constraint(name: "price_check")

См. connection.remove_check_constraint

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

Удаляет указанный внешний ключ из таблицы.

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

См. connection.remove_foreign_key

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

Удаляет указанный индекс из таблицы.

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

remove_references(*args, **options) Show source
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 876
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

Удаляет ссылку. При необходимости удаляет столбец type.

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

См. connection.remove_reference

Также является псевдонимом для: remove_belongs_to
remove_timestamps(**options) Показать исходный код
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 843
def remove_timestamps(**options)
  @base.remove_timestamps(name, **options)
end

Удаляет столбцы с отметками времени (created_at и updated_at) из таблицы.

t.remove_timestamps

См. connection.remove_timestamps

rename(column_name, new_column_name) Показать исходный код
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 852
def rename(column_name, new_column_name)
  @base.rename_column(name, column_name, new_column_name)
end

Переименовывает столбец.

t.rename(:description, :name)

См. connection.rename_column

rename_index(index_name, new_index_name) Показать исходный код
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 768
def rename_index(index_name, new_index_name)
  @base.rename_index(name, index_name, new_index_name)
end

Переименовывает указанный индекс в таблице.

t.rename_index(:user_id, :account_id)

См. connection.rename_index

timestamps(**options) Показать исходный код
# File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 777
def timestamps(**options)
  raise_on_if_exist_options(options)
  @base.add_timestamps(name, **options)
end

Добавляет столбцы с отметками времени (created_at и updated_at) в таблицу.

t.timestamps(null: false)

См. connection.add_timestamps

© 2004–2021 David Heinemeier Hansson
Licensed under the MIT License.

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API