EctoTablestore.Migration.add

You're seeing just the macro add, go back to EctoTablestore.Migration module for more information.
Link to this macro

add(column, type, opts \\ [])

View Source (macro)

Adds a primary key when creating a table.

This function only accepts types as :string | :binary | :integer | :hashids | :id.

About :auto_increment option:

  • set :auto_increment as true and its field is primary key of non-partitioned key, there will use Tablestore's auto-increment column to process it.

  • set :auto_increment as true and its field is partition key, there will use ex_aliyun_ots's built-in Sequence function, the actual principle behind it is to use the atomic update operation though another separate table when generate serial integer, by default there will add an :id partition key as :integer type, the initial value of the sequence is 0, and the increment step is 1.

Tablestore can only have up to 4 primary keys, meanwhile the first defined primary key is the partition key, Please know that the order of the primary key definition will be directly mapped to the created table.

About :hashids type to define the partition key:

  • set partition_key as true is required.
  • set auto_increment as true is required.

Examples

The auto generated serial integer for partition key:

create table("posts") do
  add :title, :string
end

# The above is equivalent to

create table("posts", partition_key: false) do
  add :id, :integer, partition_key: true, auto_increment: true
  add :title, :string
end

The explicitly defined field with partition_key:

create table("posts") do
  add :title, :string
end

# The above is equivalent to

create table("posts") do
  add :id, :integer, partition_key: true, auto_increment: true
  add :title, :string
end

The :auto_increment integer for primary key of non-partitioned key:

create table("posts") do
  add :tag, :integer, auto_increment: true
end

# The above is equivalent to

create table("posts", partition_key: false) do
  add :id, :integer, partition_key: true, auto_increment: true
  add :version, :integer, auto_increment: true
end

The :hashids type for the partition key with the built-in sequence feature:

create table("posts") do
  add :id, :hashids, auto_increment: true, partition_key: true
end

The :id type for the partition key with the built-in sequence feature:

create table("posts") do
  add :id, :id
end

# The above is equivalent to

create table("posts", partition_key: false) do
  add :id, :integer, partition_key: true, auto_increment: true
end

Options

  • :partition_key - when true, marks this field as the partition key, only the first explicitly defined field is available for this option.
  • :auto_increment - when true and this field is non-partitioned key, Tablestore automatically generates the primary key value, which is unique in the partition key, and which increases progressively, when true and this field is a partition key, use ex_aliyun_ots's Sequence to build a serial number for this field, the auto_increment: true option only allows binding of one primary key.