Module: ActiveRecord::ConnectionAdapters::PostgreSQL::ColumnMethods

Included in:
Table, TableDefinition
Defined in:
lib/active_record/connection_adapters/postgresql/schema_definitions.rb

Instance Method Summary collapse

Instance Method Details

#bigserial(*args, **options) ⇒ Object


59
60
61
# File 'lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 59

def bigserial(*args, **options)
  args.each { |name| column(name, :bigserial, options) }
end

#bit(*args, **options) ⇒ Object


63
64
65
# File 'lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 63

def bit(*args, **options)
  args.each { |name| column(name, :bit, options) }
end

#bit_varying(*args, **options) ⇒ Object


67
68
69
# File 'lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 67

def bit_varying(*args, **options)
  args.each { |name| column(name, :bit_varying, options) }
end

#box(*args, **options) ⇒ Object


143
144
145
# File 'lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 143

def box(*args, **options)
  args.each { |name| column(name, :box, options) }
end

#cidr(*args, **options) ⇒ Object


71
72
73
# File 'lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 71

def cidr(*args, **options)
  args.each { |name| column(name, :cidr, options) }
end

#circle(*args, **options) ⇒ Object


155
156
157
# File 'lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 155

def circle(*args, **options)
  args.each { |name| column(name, :circle, options) }
end

#citext(*args, **options) ⇒ Object


75
76
77
# File 'lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 75

def citext(*args, **options)
  args.each { |name| column(name, :citext, options) }
end

#daterange(*args, **options) ⇒ Object


79
80
81
# File 'lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 79

def daterange(*args, **options)
  args.each { |name| column(name, :daterange, options) }
end

#hstore(*args, **options) ⇒ Object


83
84
85
# File 'lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 83

def hstore(*args, **options)
  args.each { |name| column(name, :hstore, options) }
end

#inet(*args, **options) ⇒ Object


87
88
89
# File 'lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 87

def inet(*args, **options)
  args.each { |name| column(name, :inet, options) }
end

#int4range(*args, **options) ⇒ Object


95
96
97
# File 'lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 95

def int4range(*args, **options)
  args.each { |name| column(name, :int4range, options) }
end

#int8range(*args, **options) ⇒ Object


99
100
101
# File 'lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 99

def int8range(*args, **options)
  args.each { |name| column(name, :int8range, options) }
end

#interval(*args, **options) ⇒ Object


91
92
93
# File 'lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 91

def interval(*args, **options)
  args.each { |name| column(name, :interval, options) }
end

#json(*args, **options) ⇒ Object


103
104
105
# File 'lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 103

def json(*args, **options)
  args.each { |name| column(name, :json, options) }
end

#jsonb(*args, **options) ⇒ Object


107
108
109
# File 'lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 107

def jsonb(*args, **options)
  args.each { |name| column(name, :jsonb, options) }
end

#line(*args, **options) ⇒ Object


135
136
137
# File 'lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 135

def line(*args, **options)
  args.each { |name| column(name, :line, options) }
end

#lseg(*args, **options) ⇒ Object


139
140
141
# File 'lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 139

def lseg(*args, **options)
  args.each { |name| column(name, :lseg, options) }
end

#ltree(*args, **options) ⇒ Object


111
112
113
# File 'lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 111

def ltree(*args, **options)
  args.each { |name| column(name, :ltree, options) }
end

#macaddr(*args, **options) ⇒ Object


115
116
117
# File 'lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 115

def macaddr(*args, **options)
  args.each { |name| column(name, :macaddr, options) }
end

#money(*args, **options) ⇒ Object


119
120
121
# File 'lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 119

def money(*args, **options)
  args.each { |name| column(name, :money, options) }
end

#numrange(*args, **options) ⇒ Object


123
124
125
# File 'lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 123

def numrange(*args, **options)
  args.each { |name| column(name, :numrange, options) }
end

#oid(*args, **options) ⇒ Object


127
128
129
# File 'lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 127

def oid(*args, **options)
  args.each { |name| column(name, :oid, options) }
end

#path(*args, **options) ⇒ Object


147
148
149
# File 'lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 147

def path(*args, **options)
  args.each { |name| column(name, :path, options) }
end

#point(*args, **options) ⇒ Object


131
132
133
# File 'lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 131

def point(*args, **options)
  args.each { |name| column(name, :point, options) }
end

#polygon(*args, **options) ⇒ Object


151
152
153
# File 'lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 151

def polygon(*args, **options)
  args.each { |name| column(name, :polygon, options) }
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.timestamps
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.timestamps
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.timestamps
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.


44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 44

def primary_key(name, type = :primary_key, **options)
  options[:auto_increment] = true if [:integer, :bigint].include?(type) && !options.key?(:default)
  if type == :uuid
    options[:default] = options.fetch(:default, "gen_random_uuid()")
  elsif options.delete(:auto_increment) == true && %i(integer bigint).include?(type)
    type = if type == :bigint || options[:limit] == 8
      :bigserial
    else
      :serial
    end
  end

  super
end

#serial(*args, **options) ⇒ Object


159
160
161
# File 'lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 159

def serial(*args, **options)
  args.each { |name| column(name, :serial, options) }
end

#tsrange(*args, **options) ⇒ Object


163
164
165
# File 'lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 163

def tsrange(*args, **options)
  args.each { |name| column(name, :tsrange, options) }
end

#tstzrange(*args, **options) ⇒ Object


167
168
169
# File 'lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 167

def tstzrange(*args, **options)
  args.each { |name| column(name, :tstzrange, options) }
end

#tsvector(*args, **options) ⇒ Object


171
172
173
# File 'lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 171

def tsvector(*args, **options)
  args.each { |name| column(name, :tsvector, options) }
end

#uuid(*args, **options) ⇒ Object


175
176
177
# File 'lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 175

def uuid(*args, **options)
  args.each { |name| column(name, :uuid, options) }
end

#xml(*args, **options) ⇒ Object


179
180
181
# File 'lib/active_record/connection_adapters/postgresql/schema_definitions.rb', line 179

def xml(*args, **options)
  args.each { |name| column(name, :xml, options) }
end