Elixir-Exto:在两个不同的数据库之间验证
在我的伞形应用程序中,目前有两个应用程序,每个应用程序都基于自己的DB 我在Elixir-Exto:在两个不同的数据库之间验证,elixir,phoenix-framework,ecto,Elixir,Phoenix Framework,Ecto,在我的伞形应用程序中,目前有两个应用程序,每个应用程序都基于自己的DB 我在app2中有一个模式,引用了app1对象的uuid DB1 : Table : parent parent_id: uuid name: string DB2 : Table : child child_id: uuid parent_id: uuid name: string 然后,模式: schema "child" do field :name, :str
app2
中有一个模式,引用了app1
对象的uuid
DB1 :
Table : parent
parent_id: uuid
name: string
DB2 :
Table : child
child_id: uuid
parent_id: uuid
name: string
然后,模式:
schema "child" do
field :name, :string
belongs_to(:parent, Parent, type: :binary_id)
timestamps()
end
如何让变更集验证父id是否有效并存在于其表中?我尝试了一些方法,比如assoc\u constraint(:parent)
,但我知道它不起作用。。。也许我应该重新思考一下这段关系以及它是如何运作的
谢谢。简短的回答是:不,不同数据库之间不可能有现成的关系。因此,如果
parent
是另一个数据库中的表,则不能在Child
中使属于(:parent,parent,type::binary\u id)
答案很长,根据您的RDBMS,可以定义数据库之间的关系。例如,Postgres有一些扩展,如dblink和Postgres_fdw,这些都有简要介绍。嘿,谢谢。我知道在不同的DBs中的表之间不能很容易地使用FK,但是,编程验证应该是可能的。。。所以,即使它不能马上工作,问题是,我该怎么做才能让它工作?创建一个自定义验证函数,该函数将检索父项并进行检查?那么这个功能应该属于哪里呢?不在变更集中,这是肯定的…是的,我认为在另一个回购中进行检查的自定义验证器应该在这里工作。该验证器必须从
changeset
函数调用,并且可以在模型/架构文件中定义。