首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用cast_assoc和事务插入外键约束的两边

使用cast_assoc和事务插入外键约束的两边
EN

Stack Overflow用户
提问于 2022-01-27 17:15:54
回答 1查看 261关注 0票数 1

我有一组非常大的使用cast_assoc同时保存的互连记录,但是其中一条记录对记录集中的另一部分有一个外键约束。

代码语言:javascript
复制
  @primary_key {:id, :binary_id, autogenerate: true}
  schema "c_rtu" do
    field(:name, :string)
    has_many(:communication_maps, CommunicationMap)
    has_many(:rtu_streams, RtuStream)

    timestamps()
  end

 @primary_key {:id, :binary_id, autogenerate: true}
  schema "c_rtu_stream" do
    field(:type, :string)
    field(:type_position, :integer)
    belongs_to(:template, Template, foreign_key: :template_id)
    has_many(:collection_methods, CollectionMethod)
    belongs_to(
      :c_station_channel,
      StationChannel,
      foreign_key: :station_channel_id,
      on_replace: :update
    )
    belongs_to(:c_rtu, Rtu, foreign_key: :rtu_id, on_replace: :update)
    timestamps()
  end

@primary_key {:id, :binary_id, autogenerate: true}
  schema "c_collection_method" do
    field(:method, :string)
    belongs_to(:c_rtu_stream, RtuStream, foreign_key: :rtu_stream_id, on_replace: :update)
    belongs_to(:c_section, Section, foreign_key: :section_id, on_replace: :update)
    has_many(:instances, Instance)

    timestamps()
  end

@primary_key {:id, :binary_id, autogenerate: true}
  schema "c_instance" do
    field(:calculation_method, :string)
    belongs_to(:c_field, Field, foreign_key: :field_id, on_replace: :update)
    belongs_to(
      :c_collection_method,
      CollectionMethod,
      foreign_key: :collection_method_id,
      on_replace: :update
    )
    timestamps()
  end

@primary_key {:id, :binary_id, autogenerate: true}
  schema "c_field" do
    field(:name, :string)
    has_many(:instances, Instance)
    belongs_to(
      :c_communication_map,
      CommunicationMap,
      foreign_key: :communication_map_id,
      on_replace: :update
    )
    timestamps()
  end

@primary_key {:id, :binary_id, autogenerate: true}
  schema "c_communication_map" do
    field(:name, :string)
    field(:type, :string)
    has_many(:fields, Field)
    belongs_to(:c_rtu, Rtu, foreign_key: :rtu_id, on_replace: :update)
    timestamps()
  end

我在c_instance -> c_field (c_instance requires c_field c_instance存在)之间有一个单向外键约束。

我目前正在尝试使用c_rtu作为基础来插入它,但我遇到了这样的问题:尽管有一个分支具有c_communication_map和c_field,另一个分支有c_rtu_stream、c_collection_method和c_instance,但即使预先填充了uuid,它也会遇到c_field不存在的约束。

是否存在使用cast_assoc插入的这种循环关系?

EN

回答 1

Stack Overflow用户

发布于 2022-01-31 11:39:04

你能在这里粘贴你的代码吗!!请再次检查模式"d",我认为应该是

代码语言:javascript
复制
schema "d" do
   belongs_to :c, C
   belongs_to :e, E
end

谢谢!!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70882877

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档