Module: ActiveRecord::ConnectionAdapters::PostgreSQL::ColumnMethods
- Included in:
- Table, TableDefinition
- Defined in:
- activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb
Instance Method Summary collapse
- #bigserial(*args, **options) ⇒ Object
- #bit(*args, **options) ⇒ Object
- #bit_varying(*args, **options) ⇒ Object
- #box(*args, **options) ⇒ Object
- #cidr(*args, **options) ⇒ Object
- #circle(*args, **options) ⇒ Object
- #citext(*args, **options) ⇒ Object
- #daterange(*args, **options) ⇒ Object
- #hstore(*args, **options) ⇒ Object
- #inet(*args, **options) ⇒ Object
- #int4range(*args, **options) ⇒ Object
- #int8range(*args, **options) ⇒ Object
- #interval(*args, **options) ⇒ Object
- #json(*args, **options) ⇒ Object
- #jsonb(*args, **options) ⇒ Object
- #line(*args, **options) ⇒ Object
- #lseg(*args, **options) ⇒ Object
- #ltree(*args, **options) ⇒ Object
- #macaddr(*args, **options) ⇒ Object
- #money(*args, **options) ⇒ Object
- #numrange(*args, **options) ⇒ Object
- #oid(*args, **options) ⇒ Object
- #path(*args, **options) ⇒ Object
- #point(*args, **options) ⇒ Object
- #polygon(*args, **options) ⇒ Object
-
#primary_key(name, type = :primary_key, **options) ⇒ Object
Defines the primary key field.
- #serial(*args, **options) ⇒ Object
- #tsrange(*args, **options) ⇒ Object
- #tstzrange(*args, **options) ⇒ Object
- #tsvector(*args, **options) ⇒ Object
- #uuid(*args, **options) ⇒ Object
- #xml(*args, **options) ⇒ Object
Instance Method Details
#bigserial(*args, **options) ⇒ Object
61 62 63 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 61 def bigserial(*args, **) args.each { |name| column(name, :bigserial, ) } end |
#bit(*args, **options) ⇒ Object
65 66 67 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 65 def bit(*args, **) args.each { |name| column(name, :bit, ) } end |
#bit_varying(*args, **options) ⇒ Object
69 70 71 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 69 def (*args, **) args.each { |name| column(name, :bit_varying, ) } end |
#box(*args, **options) ⇒ Object
145 146 147 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 145 def box(*args, **) args.each { |name| column(name, :box, ) } end |
#cidr(*args, **options) ⇒ Object
73 74 75 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 73 def cidr(*args, **) args.each { |name| column(name, :cidr, ) } end |
#circle(*args, **options) ⇒ Object
157 158 159 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 157 def circle(*args, **) args.each { |name| column(name, :circle, ) } end |
#citext(*args, **options) ⇒ Object
77 78 79 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 77 def citext(*args, **) args.each { |name| column(name, :citext, ) } end |
#daterange(*args, **options) ⇒ Object
81 82 83 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 81 def daterange(*args, **) args.each { |name| column(name, :daterange, ) } end |
#hstore(*args, **options) ⇒ Object
85 86 87 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 85 def hstore(*args, **) args.each { |name| column(name, :hstore, ) } end |
#inet(*args, **options) ⇒ Object
89 90 91 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 89 def inet(*args, **) args.each { |name| column(name, :inet, ) } end |
#int4range(*args, **options) ⇒ Object
97 98 99 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 97 def int4range(*args, **) args.each { |name| column(name, :int4range, ) } end |
#int8range(*args, **options) ⇒ Object
101 102 103 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 101 def int8range(*args, **) args.each { |name| column(name, :int8range, ) } end |
#interval(*args, **options) ⇒ Object
93 94 95 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 93 def interval(*args, **) args.each { |name| column(name, :interval, ) } end |
#json(*args, **options) ⇒ Object
105 106 107 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 105 def json(*args, **) args.each { |name| column(name, :json, ) } end |
#jsonb(*args, **options) ⇒ Object
109 110 111 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 109 def jsonb(*args, **) args.each { |name| column(name, :jsonb, ) } end |
#line(*args, **options) ⇒ Object
137 138 139 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 137 def line(*args, **) args.each { |name| column(name, :line, ) } end |
#lseg(*args, **options) ⇒ Object
141 142 143 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 141 def lseg(*args, **) args.each { |name| column(name, :lseg, ) } end |
#ltree(*args, **options) ⇒ Object
113 114 115 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 113 def ltree(*args, **) args.each { |name| column(name, :ltree, ) } end |
#macaddr(*args, **options) ⇒ Object
117 118 119 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 117 def macaddr(*args, **) args.each { |name| column(name, :macaddr, ) } end |
#money(*args, **options) ⇒ Object
121 122 123 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 121 def money(*args, **) args.each { |name| column(name, :money, ) } end |
#numrange(*args, **options) ⇒ Object
125 126 127 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 125 def numrange(*args, **) args.each { |name| column(name, :numrange, ) } end |
#oid(*args, **options) ⇒ Object
129 130 131 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 129 def oid(*args, **) args.each { |name| column(name, :oid, ) } end |
#path(*args, **options) ⇒ Object
149 150 151 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 149 def path(*args, **) args.each { |name| column(name, :path, ) } end |
#point(*args, **options) ⇒ Object
133 134 135 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 133 def point(*args, **) args.each { |name| column(name, :point, ) } end |
#polygon(*args, **options) ⇒ Object
153 154 155 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 153 def polygon(*args, **) args.each { |name| column(name, :polygon, ) } end |
#primary_key(name, type = :primary_key, **options) ⇒ Object
Defines the primary key field. Use of the native PostgreSQL UUID type is supported, and can be used by defining your tables as such:
create_table :stuffs, id: :uuid do |t|
t.string :content
t.
end
By default, this will use the gen_random_uuid() function from the pgcrypto
extension. As that extension is only available in PostgreSQL 9.4+, for earlier versions an explicit default can be set to use uuid_generate_v4() from the uuid-ossp
extension instead:
create_table :stuffs, id: false do |t|
t.primary_key :id, :uuid, default: "uuid_generate_v4()"
t.uuid :foo_id
t.
end
To enable the appropriate extension, which is a requirement, use the enable_extension
method in your migrations.
To use a UUID primary key without any of the extensions, set the :default
option to nil
:
create_table :stuffs, id: false do |t|
t.primary_key :id, :uuid, default: nil
t.uuid :foo_id
t.
end
You may also pass a custom stored procedure that returns a UUID or use a different UUID generation function from another library.
Note that setting the UUID primary key default value to nil
will require you to assure that you always provide a UUID value before saving a record (as primary keys cannot be nil
). This might be done via the SecureRandom.uuid
method and a before_save
callback, for instance.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 46 def primary_key(name, type = :primary_key, **) [:auto_increment] = true if [:integer, :bigint].include?(type) && !.key?(:default) if type == :uuid [:default] = .fetch(:default, "gen_random_uuid()") elsif .delete(:auto_increment) == true && %i(integer bigint).include?(type) type = if type == :bigint || [:limit] == 8 :bigserial else :serial end end super end |
#serial(*args, **options) ⇒ Object
161 162 163 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 161 def serial(*args, **) args.each { |name| column(name, :serial, ) } end |
#tsrange(*args, **options) ⇒ Object
165 166 167 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 165 def tsrange(*args, **) args.each { |name| column(name, :tsrange, ) } end |
#tstzrange(*args, **options) ⇒ Object
169 170 171 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 169 def tstzrange(*args, **) args.each { |name| column(name, :tstzrange, ) } end |
#tsvector(*args, **options) ⇒ Object
173 174 175 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 173 def tsvector(*args, **) args.each { |name| column(name, :tsvector, ) } end |
#uuid(*args, **options) ⇒ Object
177 178 179 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 177 def uuid(*args, **) args.each { |name| column(name, :uuid, ) } end |
#xml(*args, **options) ⇒ Object
181 182 183 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 181 def xml(*args, **) args.each { |name| column(name, :xml, ) } end |