将 null 插入 ecto belongs_to 字段
Insert null into ecto belongs_to field
我有一个 ecto 模型
defmodule App.Profile do
use App.Web, :model
alias App.Repo
schema "profiles" do
belongs_to :user, App.User
field :name, :string
field :last_name, :string
field :second_surname, :string
timestamps
end
但有时我想保存到数据库并将用户设置为零,我需要在用户字段中添加一些标志吗?
我的控制器中有这段代码
changeset = Profile.changeset(%Profile{user_id: nil}, profile_params)
但是当尝试保存时我得到了这个错误
:erlang.byte_size({:user_id, "can't be blank"})
在数据库中插入 null 的最佳方法是什么?
我正在使用 postgres,当手动插入我的数据时,我可以在 user_id 字段中插入 null。
没关系,我只是从必填字段中删除了 user_id 并添加到可选字段中
@required_fields ~w(name last_name)
@optional_fields ~w(second_surname user_id)
我有一个 ecto 模型
defmodule App.Profile do
use App.Web, :model
alias App.Repo
schema "profiles" do
belongs_to :user, App.User
field :name, :string
field :last_name, :string
field :second_surname, :string
timestamps
end
但有时我想保存到数据库并将用户设置为零,我需要在用户字段中添加一些标志吗?
我的控制器中有这段代码
changeset = Profile.changeset(%Profile{user_id: nil}, profile_params)
但是当尝试保存时我得到了这个错误
:erlang.byte_size({:user_id, "can't be blank"})
在数据库中插入 null 的最佳方法是什么?
我正在使用 postgres,当手动插入我的数据时,我可以在 user_id 字段中插入 null。
没关系,我只是从必填字段中删除了 user_id 并添加到可选字段中
@required_fields ~w(name last_name)
@optional_fields ~w(second_surname user_id)