我对我的控制器运行一个简单的测试,然后预加载关联
test "shows chosen resource", %{conn: conn, country: country} do
mobile = Repo.insert! %Mobile{country_id: country.id}
mobile = Repo.preload(mobile, :country)
conn = get conn, mobile_path(conn, :show, mobile)
assert json_response(conn, 200)["data"] == %{"id" => mobile.id,
"country_code" => mobile.country_code,
"number" => mobile.number}
end
然后我收到错误**(Mariaex.Error)(1054):'字段列表'中的未知列'm0.country_code'
这是我的控制器。
`def show(conn, %{"id" => id}) do
mobile = Repo.get!(Mobile, id)
render(conn, "show.json", mobile: mobile)
end`
这是我的模特。
schema "mobiles" do
field :country_code, :string
field :number, :string
belongs_to :person, Person.Person
belongs_to :country, Person.Country
has_many :mobile_audits, Person.MobileAudit, on_delete: :delete_all
timestamps()
end
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:number, :person_id, :country_code])
|> validate_required([:number])
|> assoc_constraint(:country)
end
这是我的迁移文件。
defmodule Person.Repo.Migrations.CreateMobile do
use Ecto.Migration
def change do
create table(:mobiles, primary_key: false) do
add :id, :binary_id, primary_key: true
add :country_code, :string
add :number, :string
add :person_id, references(:persons, type: :binary_id)
timestamps()
end
create unique_index(:mobiles, [:number])
end
end
defmodule Person.Repo.Migrations.CreateCountry do
use Ecto.Migration
def change do
create table(:countries, primary_key: false) do
add :id, :binary_id, primary_key: true
add :code, :string
add :name, :string
add :iso_codes, :string
timestamps()
end
end
end
我缺少什么?我使用mix phoenix.new hello_phoenix --database mysql