Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I define an ARRAY column in a Sequel Postgresql migration?

I am creating a Sequel migration to create a new table in my PostgreSQL database. I want to define a String array column, which PostgreSQL supports.

My migration looks like this:

create_table :venues do
  primary_key :id

  String      :reference                                , :null => false
  String      :name                                     , :null => false
  String      :description                              , :null => false
  String[]    :type                                     , :null => false

  DateTime    :created_at                               , :null => false
  DateTime    :updated_at                               , :null => false
end

How can I define something like text[] in my migration?

like image 602
beatlecz Avatar asked Jul 16 '13 13:07

beatlecz


People also ask

What is a PostgreSQL array?

PostgreSQL allows columns of a table to be defined as variable-length multidimensional arrays. Arrays of any built-in or user-defined base type, enum type, or composite type can be created.

How to split an array into rows in PostgreSQL?

We can split the values of an array into rows. This process is known as array expansion. In the example of the Employees table, there are some employees with two contacts in the contact array. We can split these into separate rows. PostgreSQL provides the unnest () function that can be used for this.

How to create variable length columns in PostgreSQL?

We can create variable-length columns for a specific table. The data types defined in PostgreSQL have their own type like the character is having character [], the integer is having integer [] array, etc. If we have specified our own data types, then in the background, it will create an array type for the defined data type.

Is there an alternative syntax for one-dimensional arrays in PostgreSQL?

An alternative syntax, which conforms to the SQL standard by using the keyword ARRAY, can be used for one-dimensional arrays. pay_by_quarter could have been defined as: Or, if no array size is to be specified: As before, however, PostgreSQL does not enforce the size restriction in any case. 8.14.2. Array Value Input


Video Answer


1 Answers

You just use the column method and specify the type as a string: column :type, "text[]"

like image 52
Jeremy Evans Avatar answered Sep 21 '22 08:09

Jeremy Evans