联接表中作为附加值包含在联接表中的值

问题描述

我遇到了一个既无法解决也无法在文档和问题中找到解决方案的问题。 简而言之,我正在构建一个电子商务解决方案,其中具有以下模型:产品,变体,属性,AttributeDictionary

很快,每当用户创建字典类型的属性时,其答案都将保存在AttributeDictionary模型中。 每个变体都有许多属性。

让我们现在的数据库模式:

Console.Readline();

和型号:

变量模型

mysql> DESC variants;
+----------+------------------+------+-----+---------+----------------+
| Field    | Type             | Null | Key | Default | Extra          |
+----------+------------------+------+-----+---------+----------------+
| id       | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| quantity | int(11)          | YES  |     | NULL    |                |
+----------+------------------+------+-----+---------+----------------+

mysql> DESC variants_attributes;
+----------------------------+------------------+------+-----+---------+----------------+
| Field                      | Type             | Null | Key | Default | Extra          |
+----------------------------+------------------+------+-----+---------+----------------+
| id                         | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| variants_id                | int(10) unsigned | YES  |     | NULL    |                |
| attributes_id              | int(10) unsigned | YES  |     | NULL    |                |
| attributes_dictionaries_id | int(10) unsigned | YES  | MUL | NULL    |                |
| value                      | varchar(128)     | YES  |     | NULL    |                |
+----------------------------+------------------+------+-----+---------+----------------+

mysql> DESC attributes;
+------------+----------------------------------------------+------+-----+---------+----------------+
| Field      | Type                                         | Null | Key | Default | Extra          |
+------------+----------------------------------------------+------+-----+---------+----------------+
| id         | int(10) unsigned                             | NO   | PRI | NULL    | auto_increment |
| name       | varchar(128)                                 | YES  |     | NULL    |                |
| field_type | enum('text','number','dictionary','boolean') | YES  |     | NULL    |                |
| unit       | varchar(10)                                  | YES  |     | NULL    |                |
| required   | tinyint(1)                                   | YES  |     | 0       |                |
+------------+----------------------------------------------+------+-----+---------+----------------+

mysql> DESC attributes_dictionaries;
+---------------+------------------+------+-----+---------+----------------+
| Field         | Type             | Null | Key | Default | Extra          |
+---------------+------------------+------+-----+---------+----------------+
| id            | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| attributes_id | int(10) unsigned | YES  | MUL | NULL    |                |
| value         | varchar(255)     | YES  |     | NULL    |                |
+---------------+------------------+------+-----+---------+----------------+

产品型号:

public static relationMappings = {
    attributes: {
      relation: Model.ManyToManyRelation,modelClass: 'Attribute',join: {
        from: 'variants.id',through: {
          from: 'variants_attributes.variants_id',to: 'variants_attributes.attributes_id',extra: {
            value: 'value',attributes_dictionaries_id: 'attributes_dictionaries_id',},to: 'attributes.id',};

有一些诸如关系过滤器之类的解决方案,想知道修改功能的种类,可以让我加入属性字典值。

我想要实现的是每当我获取变体关系时,如果不为null,它将attributes_dictionaries_id解析为值

重点是在文本,数字等类型的字段中,值是平坦的,但是对于字典而言,这些值是预定义的,并存储在单独的数据库表中。

我不知道该如何解决此问题,因为该选择已保存为联合表中的ID。可以将其保存为固定值,但随后将不响应字典更改。

解决方法

设法通过变量模型属性关系映射中的以下代码解决了此问题:

 modify: (qb) => {
    qb.select(
      `attributes.*`,'value',Attribute.relatedQuery('dictionary')
        .select('dictionary.value')
        .where('dictionary.id','=',raw('attributes_dictionaries_id'))
        .as('resolvedValue')
    );
  },

我现在唯一想知道的是,我是否可以合并resolvedValuevalue并有条件地将其显示出来(一种三元符号)

return attributes_dictionaries_id ? resolvedValue : value

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...