0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Phoenix Frameworkでテーブルのデータを操作する(Ecto.Repo)

Posted at

データベース接続情報

Phoenix Frameworkのプロジェクトを作成した際に、
lib/app/repo.exが作成されているはずなので、repo.ex を使ってレコードを操作します。

repo.ex はデータベースを操作するマクロEcto.Repoを読みこみ、
dev.ex で設定したデータベース情報を参照するように設定されています。

lib/app/repo.ex
defmodule App.Repo do
  use Ecto.Repo,
    otp_app: :app,
    adapter: Ecto.Adapters.Postgres
end

レコードを挿入する

入力値チェックなどせずに、単純に挿入するだけであれば、

Repo.insert 構造体名{フィールド名: 値, フィールド名: 値} 

Repo.insert %Post{title: "title", views: 0} 

で挿入が可能です。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?