- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正从 Rails 转向 Phoenix 框架。到目前为止,这是一个相当容易的过渡。不过,Phoenix 较新,我在查找一些特定信息时遇到了麻烦:
我正在使用我的 Phoenix 应用程序作为 API 服务层。
我希望我的 UI 表单(和传入的 curl 请求)使用 virtual field
来查找关联的父模型,并使用适当的属性填充子模型的 changeset
。到目前为止,还不错:
在我的 child 模型中:
schema "child" do
field :parent_name, :string, virtual: true
belongs_to :parent, MyApp.Parent
end
...
before_insert :find_and_fill_parent
def find_and_fill_parent(changeset) do
parent = MyApp.Repo.get_by(
MyApp.Parent,
parent_name: changeset.changes.parent_name
)
changeset
|> Ecto.Changeset.put_change(:parent_id, parent.id)
end
这就是我卡住的地方。我不想让 child 在没有 parent 的情况下被创造出来。我如何以及在何处nil
检查父模型?尽管有条件语句,但我尝试过的所有操作都已无条件地阻止或允许创建。
看来我需要在检查之前预加载父模型,因为 Phoenix 旨在防止像我这样的人滥用延迟加载。不幸的是,我不知道一般正确的加载模式是什么,所以我不确定如何在这里应用它。 (如果相关的话,我正在使用 MySQL)
非常感谢有关查看位置和查看内容的提示和提示,以帮助我解决这个问题!谢谢! 😃
---编辑---
根据@Gazler 的建议,我已确保我的子模型迁移具有引用:
create table(:child) do
add :parent_id, references(:parent)
end
我还是有点迷路——我想通过父字段parent_name
("Jane Doe"
) 找到parent
, 确保父模型存在,并使用 parent_id
(1
) 关联子模型。我不确定如何使用虚拟字段触发这些操作。
因此,我不确定如何构建查找父项、建立关联和检查外键验证的结构,因为原始参数中永远不会存在外键。想法?
非常感谢。
---已解决---
使用@Gazler 的更新答案,我可以在没有虚拟属性或 before_insert 的情况下成功地在子 Controller 中检查我的父模型。
def create(conn, %{"post" => post_params}) do
user = Repo.get_by(User, %{name: post_params["name"]})
if is_nil(user) do
changeset = Post.changeset(%Post{}, post_params)
else
changeset = build(user, :posts) |> Post.changeset(post_params)
end
etc
end
这会完全按照我的需要验证传入的参数!谢谢@Gazler!
最佳答案
before_insert
可能不是执行此操作的正确位置。
您正在使用 belongs_to
关联,但是如果您在另一侧包含 has_many
关联,那么您可以使用 build/3填写父id:
build(user, :posts)
这只是一个函数,用于填充结构的 post_id
字段 - 它不会验证 user
是否实际存在。
如果您想在创建帖子之前确保用户存在,那么您可以使用 foreign_key_constraint/3在你的变更集上:
cast(comment, params, ~w(user_id), ~w())
|> foreign_key_constraint(:user_id)
这将在数据库级别确保父项的记录存在。如果您的迁移看起来像这样,您需要在数据库中创建索引:
create table(:posts) do
add :user_id, references(:user)
end
编辑
不需要虚拟属性。您的 Controller 操作应类似于:
def create(conn, %{"name" => name, "post" => post_params}) do
user = Repo.get_by(User, %{name: name})
changeset = build(user, :posts) |> Post.changeset(post_params)
case Repo.insert(changeset) do
{:ok, post} -> ...
{:error, changeset} -> #if constraint is violated then the error will be in the changeset
end
end
关于mysql - Phoenix-Framework : transform, 在创建子模型时关联和不检查 Ecto 父模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32994884/
最近几天,我们考虑使用 Solr 作为我们的首选搜索引擎。 我们需要的大多数功能都是开箱即用的,或者可以轻松配置。 然而,我们绝对需要的一项功能似乎在 Solr 中被很好地隐藏(或缺失)了。 我会试着
我是 Sequelize 的新手,并且一直在探索关联。我正在使用 mysql 5.6 并 Sequelize ^4.42.0。我正在尝试创建两个简单的表:PRJS 和 TASKS 并将一些数据插入这些
关联、聚合和组合之间有什么区别?请从实现的角度解释一下。 最佳答案 对于两个对象,Foo 和 Bar 可以定义关系 关联 - 我与一个对象有关系。 Foo 使用 Bar public class Fo
这两种 hasOne 语法有什么区别? class Project { ....... ............ static hasOne = Employee // static h
对于当前的项目,我想使用遗传算法 - 目前我查看了 jenetics 库。 如何强制某些基因相互依赖?我想将 CSS 映射到基因上,例如我有基因指示是否显示图像,以及如果它也是各自的高度和宽度。因此,
关联、聚合和组合之间有什么区别?请从实现的角度解释一下。 最佳答案 对于两个对象,Foo 和 Bar 可以定义关系 关联 - 我与一个对象有关系。 Foo 使用 Bar public class Fo
假设我有一个名为“学生”的表格,其中包含姓名、手机、电子邮件、首选类(class)、首选学校、性别、年龄、地址、资格、职称、家庭电话、工作电话等列 我想从 Students 表中选择数据并插入到 2
问题标题有点困惑。我有一级员工和一级项目。一名或多名员工正在从事一个或多个项目。在这个关联中,我只有一个从具有*多重性的员工类到具有*多重性的项目类的链接。现在有另一种实现。每个项目只有一名经理,属于
到目前为止,我有一个程序采用一组随机点、站点,并围绕这些点形成适当的 Voronoi 图,表示为角和边的图形。它还为我提供了 Delaunay 三角剖分作为另一个以所有站点为节点的图形(尽管我不知道这
实现IComMethodEvents时你得到三个事件。 OnMethodCall OnMethodException OnMethodReturn 我的目标是记录 COM+ 组件中每个方法的调用时间。
我正在处理这个问题。我正在创造数学问题,每一个都有回应。例如。 如果我的问题是关于“5x + 15 = 2 的结果?”,我将只等待一个答案(整数)。 如果我的问题是关于“给我这个形状的面积和许可”,我
我正在寻找一种数据结构来保存唯一元素的无序集合,它将支持以下操作 在集合中任意位置插入/删除元素 查询元素是否存在 访问一个随机元素 天真地,1 和 2 建议使用关联容器,例如unordered_se
是否可以在 LINQ 中使用类似 ContactAddress.Contact 的内容,而无需在 SQL Server 中在这两者之间创建外键关系(通过 Contact.Id ContactAddr
我一直在谷歌搜索,但不明白调用 javax.persistence.criteria.Subquery 和 Criteria API 的方法相关的结果是什么。 http://www.objectdb.
我正在关注 Chris McCord 的“Programming Phoenix”一书,在第 6 章中,在 User 之间创建了一个关系。和一个 Video . 尝试使用 mix phoenix.se
我在 XAML 中有一个 ItemsControl,我在其中为每个组显示一个扩展器,以便我可以展开/折叠该组。我想保持 IsExpanded 的状态属性(以及可能与组标题显示相关的其他设置)。通常你只
Oracle 11 中是否有内置方法来检查 varchar2 字段中值的相关性?例如,给定一个简单的表,如下所示: MEAL_NUM INGREDIENT --------------------
是否可以在没有 JPA 在数据库中创建外键的情况下设置多对一关联? 这些表归另一个系统所有,并以异步方式填充。因此我们不能在数据库中使用 FK。仍然,几乎总是,最终是一种关系。 @ManyToOne(
我一直在使用NHibernate,使用Fluent NHibernate进行映射。我解决了很多问题,并开始认为自己在nhibernate中经验丰富。 但是,此错误非常奇怪。 这是我的模型: p
我正在开发一个 Typescript Sequelize 项目,其中我的 /models/index.ts 文件具有以下“导入此目录中的所有模型”功能: var basename = path.bas
我是一名优秀的程序员,十分优秀!