Creating hybrid schemas in Ecto 3?

Let’s say I have two tables, “users” and “user_details” and want to create a hybrid model that contains data from both.

What I have:

users:

  • id
  • name
  • email
  • password

user_details:

  • id
  • user_id
  • favorite_color
  • cats_or_dogs

What I want:

super_users:

  • user_id
  • user_details_id(maybe?)
  • email
  • favorite_color
  • cats_or_dogs

How could I accomplish this in Ecto?

Currently I have a Phoenix app and have been trying to cobble this together in the context. Being relatively new to Elixir, it’s not pretty. I would really like to learn how to do this the Elixir way.

I can create an example app if it would help anybody help me. Thanks!

Are ecto associations sufficient?
It looks like User has_one UserDetails and UserDetails belongs_to User.

2 Likes

This seems like a good candidate for a DB view which Ecto supports natively.

3 Likes

They should be. I’ve probably been trying to overcomplicate things. Thanks!

1 Like