Method: Sequel::Schema::CreateTableGenerator#column
- Defined in:
- lib/sequel/database/schema_generator.rb
permalink #column(name, type, opts = OPTS) ⇒ Object
Add a column with the given name, type, and opts:
column :num, :integer
# num INTEGER
column :name, String, null: false, default: 'a'
# name varchar(255) NOT NULL DEFAULT 'a'
inet :ip
# ip inet
You can also create columns via method missing, so the following are equivalent:
column :number, :integer
integer :number
The following options are supported:
- :collate
-
The collation to use for the column. For backwards compatibility, only symbols and string values are supported, and they are used verbatim. However, on PostgreSQL, symbols are literalized as regular identifiers, since unquoted collations are unlikely to be valid.
- :default
-
The default value for the column.
- :deferrable
-
For foreign key columns, this ensures referential integrity will work even if referencing table uses a foreign key value that does not yet exist on referenced table (but will exist before the transaction commits). Basically it adds DEFERRABLE INITIALLY DEFERRED on key creation. If you use :immediate as the value, uses DEFERRABLE INITIALLY IMMEDIATE.
- :generated_always_as
-
Specify a GENERATED ALWAYS AS column expression, if generated columns are supported (PostgreSQL 12+, MariaDB 5.2.0+, and MySQL 5.7.6+).
- :index
-
Create an index on this column. If given a hash, use the hash as the options for the index.
- :key
-
For foreign key columns, the column in the associated table that this column references. Unnecessary if this column references the primary key of the associated table, except if you are using MySQL.
- :null
-
Mark the column as allowing NULL values (if true), or not allowing NULL values (if false). The default is to allow NULL values.
- :on_delete
-
Specify the behavior of this column when being deleted (:restrict, :cascade, :set_null, :set_default, :no_action).
- :on_update
-
Specify the behavior of this column when being updated (:restrict, :cascade, :set_null, :set_default, :no_action).
- :primary_key
-
Make the column as a single primary key column. This should not be used if you want a single autoincrementing primary key column (use the primary_key method in that case).
- :primary_key_constraint_name
-
The name to give the primary key constraint
- :primary_key_deferrable
-
Similar to :deferrable, but for the primary key constraint if :primary_key is used.
- :type
-
Overrides the type given as the argument. Generally not used by column itself, but can be passed as an option to other methods that call column.
- :unique
-
Mark the column as unique, generally has the same effect as creating a unique index on the column.
- :unique_constraint_name
-
The name to give the unique key constraint
- :unique_deferrable
-
Similar to :deferrable, but for the unique constraint if :unique is used.
PostgreSQL specific options:
- :identity
-
Create an identity column.
MySQL specific options:
- :generated_type
-
Set the type of column when using :generated_always_as, should be :virtual or :stored to force a type.
- :on_update_current_timestamp
-
Use ON UPDATE CURRENT TIMESTAMP when defining the column, which will update the column value to CURRENT_TIMESTAMP on every UPDATE.
Microsoft SQL Server specific options:
- :clustered
-
When using :primary_key or :unique, marks the primary key or unique constraint as CLUSTERED (if true), or NONCLUSTERED (if false).
157 158 159 160 161 162 163 |
# File 'lib/sequel/database/schema_generator.rb', line 157 def column(name, type, opts = OPTS) columns << {:name => name, :type => type}.merge!(opts) if index_opts = opts[:index] index(name, index_opts.is_a?(Hash) ? index_opts : OPTS) end nil end |