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?
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.
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.
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.
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
You just use the column method and specify the type as a string: column :type, "text[]"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With