dwyl / learn-phoenix

:fire: Phoenix is the web framework without compromise on speed, reliability or maintainability! Don't settle for less. :rocket:
650 stars 45 forks source link

convert sql query to ecto #140

Closed katpavan closed 3 years ago

katpavan commented 3 years ago

INSERT INTO messages (message) VALUES ('one message'), ('two message'), ('three message')

I have a phoenix app where I scaffolded messages. I want to be able to insert multiple messages at the same time.

Right now i'm using a transaction to insert multiple messages into the database but I want to do this more efficiently.

katpavan commented 3 years ago

Issue solved:

Repo.insert_all fixed my issue.

One problem with insert_all is that inserted_at and updated_at don't get set, so you have to manually set that.

you can get the current timestamp by doing

now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)