Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you dynamically set a schema table name for a ecto model

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 SCHEMA table name?

like image 370
adam Avatar asked Nov 18 '16 22:11

adam


1 Answers

Most of the functions that expect a schema, also accept a tuple in the form of {"source", Schema}, where source is the table name. The source is also properly maintained by structs, so if you select from a particular source, when you update/delete it will resolve to the proper table. There's also Ecto.put_meta(struct, source: "source") for annotating already build structs.

Those features were also recently expanded to cover more use cases, you can see more in this issue: #1797.

like image 190
michalmuskala Avatar answered Sep 22 '22 05:09

michalmuskala