Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
OrganizationAdvent CalendarQiitadon (β)
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

Ecto の virtual field で struct を参照する

More than 1 year has passed since last update.

Ecto.Schema のフィールドを定義するときに、struct を参照する virtual なフィールドを定義したい。

field :my_struct, MyStruct, virtual: true


== Compilation error in file ... ==
** (ArgumentError) schema MyStruct is not a valid type for field :my_struct. Did you mean to use belongs_to, has_one, has_many, embeds_one, or embeds_many instead?

かといって、has_one/3 などのリファレンスを読んでも virtual オプションはない。結局、タイプを :any とすることで切り抜けたが、型を明示するためにもできれば struct を指定したいところ。

field :my_struct, :any, virtual: true
Developer in Tokyo ❤️Elixir
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away