- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
尝试插入新房间时出现以下错误
** (Ecto.ConstraintError) constraint error when attempting to insert struct:
* unique: rooms_pkey
If you would like to convert this constraint into an error, please
call unique_constraint/3 in your changeset and define the proper
constraint name. The changeset defined the following constraints:
* unique: rooms_name_index
主键不应该自增吗?什么会导致这个错误突然发生?插入是作为 multi 的一部分完成的,相关部分是:
|> Multi.insert(:room, Room.changeset(%Room{}, %{name: "service-user-care-team:" <> Integer.to_string(user.id)}))
作为额外引用,这是我的架构,包括变更集
schema "rooms" do
field :name, :string
many_to_many :users, App.User, join_through: "user_rooms", on_delete: :delete_all
has_many :messages, App.Message
timestamps()
end
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:name])
|> validate_required([:name])
|> unique_constraint(:name)
end
这是迁移
defmodule App.Repo.Migrations.CreateRoom do
use Ecto.Migration
def change do
create table(:rooms) do
add :name, :string, null: false
timestamps()
end
create unique_index(:rooms, [:name])
end
end
最佳答案
我找到了发生这种情况的原因。
我忘记在原始描述中包含的重要说明是,这是在开发环境中工作时发生的。
与this answer有关.我之前用 Postico 手动插入了一些数据,其中必须明确包含 id。 pkey 序列在那一点上没有更新,后来导致设置了重复的 id。
关于postgresql - pkey 上的 Ecto 唯一约束错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46475815/
docs状态 run(t, name, module, function, args) :: t when function: atom, args: [any] Similar to run/3,
目前,我正在寻找在运行 mix 任务时添加 SQL 语句日志记录的方法。例如,mix ecto.rollback 和 mix ecto.migrate 等命令输出信息: ... 13:45:53.01
我有两张 table 。 topics的表其中has_many tweets .我的表tweets belongs_to topic . 主题架构: defmodule Sentiment.Topic
今天,和往常一样,我尝试制作 混合 ecto.migrate 在我的 Phoenix 应用程序中,意外地发现了以下错误: warning: could not find repositories fo
我可以使用什么函数来检查关联是否已经加载? 最好检查关联是否已加载,而不是尝试使用它并获取 Ecto.Association.NotLoaded错误。 最佳答案 您可以使用 assoc_loaded?
我在 postgresql 中有一个表,我想用一个随机数更新所有记录的“points”列的值。在其他语言中,我们可以循环遍历所有数据库记录,但我如何使用 ecto 来做到这一点?我试过这个: Rep
在处理具有关联的模型时,加载关联模型的最佳实践是什么:显式使用 Repo.preload 或指定在定义查询时应预加载的关联? 最佳答案 它们是等效的,因此任何方法都可以,真的。如果您想将所有内容都放在
有什么办法可以逆向预加载吗? %Post{ comments: [] } posts = Repo.all(Post) |> Repo.unload(:comments) %Post{ com
使用 Ecto 访问大写字段的最佳方式是什么? 我必须处理一个相当困惑的模式。有些列是大写的。 我设置了以下模型: defmodule SourcesApi.SourceStatus do use
在下面的链接中做了一些研究之后 https://github.com/elixir-lang/ecto/tree/master/examples/simple 我对如何在 elixir 中使用 ect
有没有办法通过选择另一个连接列来预加载记录? # table structure # User 1---* Post 1---* PostTag *---1 Tag # extract definit
考虑以下架构: defmodule EctoBug.Post do use Ecto.Schema import Ecto.Changeset schema "posts" do
这些都不起作用: from m in Model, where: m.name == ^~r(/.*#{query}.*/i)from m in Model, where: m.name =~ ^~r
假设我有这些模式: defmodule Sample.Post do use Ecto.Schema schema "post" do field :title has_man
我有一个针对多个数据库的应用程序,它就像一个监视应用程序的应用程序,它以一种不可能静态控制的方式(在配置文件中)生成报告、调解通知、运行任务和东西,所以我认为它可以启动一个监督多个 Repo 的主管来
我正在尝试编写单个变更集,以更新模型并插入关联。我找不到有关如何使用 put_assoc/4 的示例 order = order |> Proj.Order.changeset(%{sta
我正在寻找所有 User在其 match_history 中没有特定字符串元素的 s field 。我对此进行了猜测: matched_user = User |> where([u], ^devic
我无法成功运行 Ecto 迁移以删除最初创建时提供的唯一索引 :name属性(以便不使用默认索引名称)。但是,我现在无法删除该索引,因为似乎 Ecto 正在尝试查找名称不正确的索引(尽管我已经提供了它
为什么在 Ecto changeset方法您是否将参数设置为默认值:empty原子?例如 def changeset(user, params \\ :empty) do ... 这是否允许您使
注意:我使用的是 MySQL 5.7。 我有两个名为 Post、Comment 的表 schema "posts" do field: id, string field :titl
我是一名优秀的程序员,十分优秀!