Multipe Ecto tables?

hey, so is it possible to create multiple tables and then save data to them?
like one function, that saves data to tableX and one for tableY?

def change do
     create table(:accounts) do
      add ...

      timestamps
     end

    create table(:somethingelse) do 
      add ...

  end

Can you create multiple tables in a migration? Yes. You can create/remove/modify whatever you like in there really.

Can you write a seed script that adds data to multiple tables? Yes.

Can you write a function that adds data to those tables/schemas at runtime? Yes. Repo.transaction and Ecto.Multi will do it in a transaction. There is nothing stopping you doing it outside of a transaction (except application requirements, e.g. consistency).

1 Like

I will advise at a higher level

  1. One table per migration for maintainability, do not mix them in the same file

  2. Do not seed data in migration, run it in a script separately

3 Likes