代码之家  ›  专栏  ›  技术社区  ›  Neil McGuigan

实体两次属于同一实体时的关系(JS数据)

  •  0
  • Neil McGuigan  · 技术社区  · 4 年前

    想象一个销售订单模型,如下所示:

    create table sales_orders(
      id int primary key,
      order_date date,
      customer_party_id int not null references parties,
      sales_party_id int not null references parties
    );
    
    create table parties (
      id int primary key,
      type text check (type in 'individuals', 'organizations'),
      given_name text,
      surname text,
      org_name text
    );
    

    在这种情况下,如何对JSData关系进行建模?

    很明显,我不能这样做:

    store.defineMapper("sales_orders", {
        relations:{
            belongsTo:{
                parties:{
                    foreignKey:"customer_party_id",
                    localField:"customer_party"
                },
                parties:{
                    foreignKey:"sales_party_id",
                    localField:"sales_party"
                }
            }
        }
    });
    

    我可以重命名其中一个belongsTo字段,这适用于读取,但不适用于写入。。。(使用JSONAPI适配器)

    0 回复  |  直到 4 年前
        1
  •  0
  •   Justin Grant    4 年前

    我不知道这是否算作答案,但这是我唯一可以放代码的地方。:)我不清楚是什么在消耗你发送的对象,但我希望你能够将你的各方作为数组传递,如下所示:

    store.defineMapper("sales_orders", {
    relations:{
        belongsTo:{
            parties:[{
                foreignKey:"customer_party_id",
                localField:"customer_party"
            },
            {
                foreignKey:"sales_party_id",
                localField:"sales_party"
            }]
        }
    }
    

    });

    这是一种选择吗?

        2
  •  0
  •   Tch    4 年前

    好吧,既然你有两把外键,为什么还要两把 type 在您的聚会桌上,将客户与销售区分开来?除非此列用于其他用途,否则您可以再创建一个ex。 party_type 为了这个目的。

    我建议您删除其中一列,将其余列重命名为 party_id 然后,您应该为这两个表定义eMapper,如下所示

    store.defineMapper('parties', {
        relations: {
            hasMany: {
                sales_orders: {
                    foreignKey: 'party_id',
                    localField: 'party'
                }
            }
        }
    });
    store.defineMapper('sales_orders', {
        relations: {
            belongsTo: {
                parties: {
                    foreignKey: 'party_id',
                    localField: 'party'
                }
            }
        }
    });
      
    
    推荐文章