Dynamically set the schema tablename for model

541 views
Skip to first unread message

Adam Boas

unread,
Nov 19, 2016, 11:00:36 PM11/19/16
to elixir-ecto

I have a model that I would like to partition across multiple database tables. All the data and functionality will be the same so I would like to use the same module/ecto.model but just have the table it is saved into differ. Further, I won't know until runtime what the table name will be.

Can anyone suggest a way to dynamically set the table name that  model saves to and retrieves from?


Cheers,


Adam

José Valim

unread,
Nov 20, 2016, 3:48:13 AM11/20/16
to elixi...@googlegroups.com
Ecto.put_meta is what you want. For example, to get a struct from its "original" table and mark it as built to save on another table:

post = Repo.get!(Post, 1)
Ecto.put_meta post, source: "new_table", state: :built
Repo.insert!(post)



José Valim
Skype: jv.ptec
Founder and Director of R&D

--
You received this message because you are subscribed to the Google Groups "elixir-ecto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/a8e3af8f-0091-41a9-9ce2-de9298ce3bda%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Adam Boas

unread,
Nov 21, 2016, 12:56:30 AM11/21/16
to elixi...@googlegroups.com
Thanks for that. That looks great for doing inserts. Can I use a similar strategy for querying or do I need to resort to raw SQL?

Cheers,

Adam
You received this message because you are subscribed to a topic in the Google Groups "elixir-ecto" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elixir-ecto/6Tzn7C58ECg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elixir-ecto...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/CAGnRm4KJNvdek01RP3cc6C_VtjKtOEjbQFpkSenbMOfku1AYzA%40mail.gmail.com.

Michał Muskała

unread,
Nov 21, 2016, 1:02:06 AM11/21/16
to elixi...@googlegroups.com
You can use 'from {"table", Schema}' instead of 'from Schema' to specify the table.
The returned structs will have the source meta field properly set, so subsequent updates/deletes go to the proper place.

Michał.
> On 21 Nov 2016, at 06:56, Adam Boas <adam...@gmail.com> wrote:
>
> Thanks for that. That looks great for doing inserts. Can I use a similar strategy for querying or do I need to resort to raw SQL?
>
> Cheers,
>
> Adam
>
>
> On 20 November 2016 at 7:48:14 pm, José Valim (jose....@plataformatec.com.br) wrote:
>
>> Ecto.put_meta is what you want. For example, to get a struct from its "original" table and mark it as built to save on another table:
>>
>> post = Repo.get!(Post, 1)
>> Ecto.put_meta post, source: "new_table", state: :built
>> Repo.insert!(post)
>>
>>
>>
>> José Valim
>> www.plataformatec.com.br
>> Skype: jv.ptec
>> Founder and Director of R&D
>>
>> On Sun, Nov 20, 2016 at 5:00 AM, Adam Boas <adam...@gmail.com> wrote:
>> I have a model that I would like to partition across multiple database tables. All the data and functionality will be the same so I would like to use the same module/ecto.model but just have the table it is saved into differ. Further, I won't know until runtime what the table name will be.
>>
>> Can anyone suggest a way to dynamically set the table name that model saves to and retrieves from?
>>
>>
>>
>> Cheers,
>>
>>
>>
>> Adam
>>
>> --
>> You received this message because you are subscribed to the Google Groups "elixir-ecto" group.
>> To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto...@googlegroups.com.
>> To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/a8e3af8f-0091-41a9-9ce2-de9298ce3bda%40googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>>
>> --
>> You received this message because you are subscribed to a topic in the Google Groups "elixir-ecto" group.
>> To unsubscribe from this topic, visit https://groups.google.com/d/topic/elixir-ecto/6Tzn7C58ECg/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to elixir-ecto...@googlegroups.com.
>> To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/CAGnRm4KJNvdek01RP3cc6C_VtjKtOEjbQFpkSenbMOfku1AYzA%40mail.gmail.com.
>> For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to the Google Groups "elixir-ecto" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/CANj6fzHu0JaaJ9fNSWv%3DFC7oF39O5TZsx7g7zOoDpsTf1a26Xw%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages