我有以下架构:
@primary_key false
schema "companies" do
field :number, :integer, primary_key: true, read_after_writes: true
field :name, :string
field :street, :string
has_many :contacts, Busiket.Contact
timestamps
end
schema "contacts" do
field :firstname, :string
field :lastname, :string
belongs_to :company, Busiket.Company, foreign_key: :company, references: :number, type: :integer
timestamps
end
我有以下错误:
** (ArgumentError) need to set :references option for association :contacts when schema has no primary key
lib/ecto/association.ex:329: Ecto.Association.Has.struct/3
lib/ecto/schema.ex:1434: Ecto.Schema.association/5
lib/ecto/schema.ex:1249: Ecto.Schema.__has_many__/4
web/models/company.ex:18: (module)
(stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
迁移看起来:
create table(:companies, primary_key: false) do
add :number, :serial, primary_key: true
add :name, :string
add :street, :string
add :zipcode, :integer
add :location, :string
add :phone, :integer
add :company_class, references(:companies_classes, column: :code, type: :integer)
add :country_iso, references(:countries_codes, column: :iso, type: :string)
add :email, :string
add :password_hash, :string
timestamps
end
execute "select setval(pg_get_serial_sequence('companies', 'number'), 999999)"
create unique_index(:companies, [:name, :email])
create table(:contacts) do
add :firstname, :string
add :lastname, :string
add :title, :string
add :email, :string
add :password_hash, :string
add :company, references(:companies, column: :number, type: :integer, on_delete: :delete_all)
timestamps
end
create unique_index(:contacts, [:email])
我做错了什么?