Elixir Exto自定义主键
我正在尝试将主键从Elixir Exto自定义主键,elixir,ecto,Elixir,Ecto,我正在尝试将主键从:id重命名为:account\u id 我有以下模式和迁移。当我尝试插入一条记录时,我从Postgres的account\u id列中得到一个“cannot be null”错误 我是否正确设置了自动生成位?我觉得这应该是在迁移中 def change do create table(:accounts) do add :account_id, :integer, primary_key: true add :email, :string
:id
重命名为:account\u id
我有以下模式
和迁移。当我尝试插入一条记录时,我从Postgres的account\u id列中得到一个“cannot be null”错误
我是否正确设置了自动生成位?我觉得这应该是在迁移中
def change do
create table(:accounts) do
add :account_id, :integer, primary_key: true
add :email, :string
timestamps()
end
end
@primary_key {:account_id, :id, autogenerate: true}
schema "accounts" do
field :email, :string
timestamps()
end
您应该传递
主键:false
选项:
def change do
create table(:accounts, primary_key: false) do
add :account_id, :integer, primary_key: true
add :email, :string
timestamps()
end
end
[编辑]
正如提问者自己在回答中所说,我们可以使用:bigserial
而不是:integer
:bigserial
需要8个字节,:integer
需要4个字节
从Ecto 2.0(2016年发布)开始,主键的默认类型为:bigserial
,
但是我们可以安全地将
:integer用于普通用途,因为它仍然非常大(1到2147483647)。您应该传递主键:false
选项:
def change do
create table(:accounts, primary_key: false) do
add :account_id, :integer, primary_key: true
add :email, :string
timestamps()
end
end
[编辑]
正如提问者自己在回答中所说,我们可以使用:bigserial
而不是:integer
:bigserial
需要8个字节,:integer
需要4个字节
从Ecto 2.0(2016年发布)开始,主键的默认类型为:bigserial
,
但是我们可以安全地将:integer用于普通用途,因为它仍然非常大(1到2147483647)。这在文档或其他指南中并不明显。诀窍是为:id
字段键入:bigserial
def change do
create table(:accounts, primary_key: false) do
add :account_id, :bigserial, primary_key: true
add :email, :string
timestamps()
end
end
这在文档或其他指南中并不明显。诀窍是为:id
字段键入:bigserial
def change do
create table(:accounts, primary_key: false) do
add :account_id, :bigserial, primary_key: true
add :email, :string
timestamps()
end
end
:account\u id应该是类型:integer,还是类型:id?account\u id应该是类型:integer,还是类型:id?