要解決的問題
資料庫中儲存的行數超過了
serial
型別所能容納的數量, 因此需要採用bigserial
型別的整數作為主鍵serial
的取值範圍為:1 到 2147483647
bigserial
的取值範圍為:1 到 9223372036854775807
完整的 Postgresql 欄位的資料型別, 可以參考這裡
遷移指令碼
defmodule ElectricProto.Repo.Migrations.AddStationTable do
use Ecto.Migration
def up do
create table(:station, primary_key: false) do
add :id, :bigserial, primary_key: true
timestamps
end
end
def down do
drop table(:station)
end
end
要點
create table
的引數primary_key
要設定為false
,通過
add
巨集指定主鍵列id
, 型別為bigserial
模型的宣告
@primary_key {:id, :id, autogenerate: true}
schema "station" do
field :area, :string, default: ""
field :carrier, :string, default: ""
field :city, :string, default: ""
field :deployed, :boolean, default: false
field :description, :string, default: ""
field :device_auth, :string, default: ""
field :device_type, :string, default: ""
field :geolocation, :string, default: ""
field :ip_addr, :string, default: ""
field :qrcode, :string, default: ""
field :station_id, :string, default: ""
field :status, :string, default: ""
timestamps
end
要點
主鍵要宣告為
:id
型別,@primary_key {:id, :id, autogenerate: true}
完!