instance method
create_table
Ruby on Rails 3.1.12
Since v2.2.3Signature
create_table(table_name, options = {})
Creates a new table with the name table_name. table_name may either be a String or a Symbol.
There are two ways to work with create_table. You can use the block form or the regular form, like this:
Block form
# 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
Block form, with shorthand
# 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
Regular form
# Creates a table called 'suppliers' with no columns. create_table(:suppliers) # Add a column to 'suppliers'. add_column(:suppliers, :name, :string, {:limit => 60})
The options hash can include the following keys:
:id-
Whether to automatically add a primary key column. Defaults to true. Join tables for
has_and_belongs_to_manyshould set it to false. :primary_key-
The name of the primary key, if one is to be added automatically. Defaults to
id. If:idis false this option is ignored.Also note that this just sets the primary key in the table. You additionally need to configure the primary key in the model via the
set_primary_keymacro. Models do NOT auto-detect the primary key from their table definition. :options-
Any extra options you want appended to the table definition.
:temporary-
Make a temporary table.
:force-
Set to true to drop the table before creating it. Defaults to false.
Examples
Add a backend specific option to the generated SQL (MySQL)
create_table(:suppliers, :options => 'ENGINE=InnoDB DEFAULT CHARSET=utf8')
generates:
CREATE TABLE suppliers ( id int(11) DEFAULT NULL auto_increment PRIMARY KEY ) ENGINE=InnoDB DEFAULT CHARSET=utf8
Rename the primary key column
create_table(:objects, :primary_key => 'guid') do |t| t.column :name, :string, :limit => 80 end
generates:
CREATE TABLE objects ( guid int(11) DEFAULT NULL auto_increment PRIMARY KEY, name varchar(80) )
Do not add a primary key column
create_table(:categories_suppliers, :id => false) do |t| t.column :category_id, :integer t.column :supplier_id, :integer end
generates:
CREATE TABLE categories_suppliers ( category_id int, supplier_id int )
See also TableDefinition#column for details on how to create columns.
Parameters
-
table_namereq -
optionsopt = {}
Source
# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 157
def create_table(table_name, options = {})
td = table_definition
td.primary_key(options[:primary_key] || Base.get_primary_key(table_name.to_s.singularize)) unless options[:id] == false
yield td if block_given?
if options[:force] && table_exists?(table_name)
drop_table(table_name, options)
end
create_sql = "CREATE#{' TEMPORARY' if options[:temporary]} TABLE "
create_sql << "#{quote_table_name(table_name)} ("
create_sql << td.to_sql
create_sql << ") #{options[:options]}"
execute create_sql
end
Defined in activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb line 157
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::ConnectionAdapters::SchemaStatements