【问题标题】:Strongloop PostgreSQL connector embedded model errorStrongloop PostgreSQL 连接器嵌入模型错误
【发布时间】:2015-12-13 07:49:32
【问题描述】:

获得了一个简单的 Loopback API 来使用 postgreSQL 连接器检索票证和响应。票证和响应单独返回很好,但是当我尝试将响应嵌入票证模型时,我收到以下错误。我已经尝试按照文档进行操作,并且我确信在我缺少的一个关系中这很简单,但是无论我尝试什么,我都无法让它发挥作用。

任何帮助将不胜感激。

https://docs.strongloop.com/display/public/LB/Embedded+models+and+relations#Embeddedmodelsandrelations-EmbedsMany

票种:

{
"name": "Ticket",          
"base": "PersistedModel",
"idInjection": true,
"options": {
    "postgresql": {
        "schema": "customer_service",
        "table": "tbl_ticket"
     }
},
"properties": {
    "description": {
      "type": "String",
      "required": true,
      "length": null,
      "precision": null,
      "scale": null,
      "postgresql": {
        "columnName": "description",
        "dataType": "text",
        "dataLength": null,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "NO"
      }
    },    
    "id": {
      "type": "Number",
      "id": 1,
      "required": true,
      "length": null,
      "precision": 32,
      "scale": 0,
      "postgresql": {
        "columnName": "id",
        "dataType": "integer",
        "dataLength": null,
        "dataPrecision": 32,
        "dataScale": 0,
        "nullable": "NO"
      }
    }
  },
  "validations": [],
  "relations": {
    "responses": {
      "type": "embedsMany",
      "model": "Response",
      "property": "embededResponses",
      "options": {
        "validate": true,
        "forceId": false
      }   
    }
  },
  "acls": [],
  "methods": {}
}

响应模型:

{
  "name": "Response",
  "base": "PersistedModel",
  "idInjection": true,  
  "options": {
    "postgresql": {
      "schema": "customer_service",
      "table": "tbl_response"
    }
  },
  "properties": {
    "notes": {
      "type": "String",
      "required": false,
      "length": null,
      "precision": null,
      "scale": null,
      "postgresql": {
        "columnName": "notes",
        "dataType": "text",
        "dataLength": null,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "YES"
      }
    },
    "ticketId": {
      "type": "Number",
      "required": true,
      "length": null,
      "precision": 32,
      "scale": 0,
      "postgresql": {
        "columnName": "ticket_id",
        "dataType": "integer",
        "dataLength": null,
        "dataPrecision": 32,
        "dataScale": 0,
        "nullable": "NO"
      }
    },
    "id": {
      "type": "Number",
      "id": 1,
      "required": true,
      "length": null,
      "precision": 32,
      "scale": 0,
      "postgresql": {
        "columnName": "id",
        "dataType": "integer",
        "dataLength": null,
        "dataPrecision": 32,
        "dataScale": 0,
        "nullable": "NO"
      }
    }
  },
  "validations": [],
  "relations": {
    "ticket": {
      "type": "belongsTo",
      "model": "Ticket",
      "foreignKey": "ticketId"
    }
  },
  "acls": [],
  "methods": {}
}

错误:

{  
    "error": {
      "name": "error",
      "status": 500,
      "message": "column \"embededresponses\" does not exist",
      "length": 126,
      "severity": "ERROR",
      "code": "42703",
      "position": "213",
      "file": ".\\src\\backend\\parser\\parse_expr.c",
      "line": "766",
      "routine": "transformColumnRef",
      "stack": "error: column \"embededresponses\" does not exist\n    at Connection.parseE (C:\\WebApp\\node_modules\\loopback-connector-postgresql\\node_modules\\pg\\lib\\connection.js:539:11)\n    at Connection.parseMessage (C:\\WebApp\\node_modules\\loopback-connector-postgresql\\node_modules\\pg\\lib\\connection.js:366:17)\n    at Socket.<anonymous> (C:\\WebApp\\node_modules\\loopback-connector-postgresql\\node_modules\\pg\\lib\\connection.js:105:22)\n    at Socket.emit (events.js:107:17)\n    at readableAddChunk (_stream_readable.js:163:16)\n    at Socket.Readable.push (_stream_readable.js:126:10)\n    at TCP.onread (net.js:538:20)"
    }
}

【问题讨论】:

  • 我在该模型中看不到 embededResponses 属性。该错误似乎表明这不存在。
  • 我确实尝试过将我的票证模型的 embededResponses 属性声明为对象和数组,但仍然得到相同的错误。如果您在 GitHub 上查看他们的示例,您似乎不需要这样做。 github.com/strongloop/loopback-example-embedded-relations/blob/…

标签: loopbackjs strongloop


【解决方案1】:

您的Ticket 模型应具有以下关系部分:

"relations": {
    "Responses": {
      "type": "hasMany",
      "model": "Response",
      "foreignKey": "ticketId"
    }
  }

您的Response 模型关系正确。

从文档中并不清楚嵌入关系是否适用于 NoSQL 数据库。对于传统 SQL 数据库,请使用 Has* 关系类型。

要从 REST API 检索带有 ResponsesTicket,请使用包含过滤器:https://docs.strongloop.com/display/public/LB/Include+filter

示例:localhost:3000/api/Tickets/{id}?filter[include]=responses

【讨论】:

  • 请求成功返回,但响应没有嵌入到 Ticket 模型中,我仍然需要发出两个 GET 请求(localhost:3000/api/Tickets/{id} & localhost:3000/api/ Tickets/{id}/responses) 返回两组数据。有没有办法做到这一点,所以我只需要发出一个 GET 请求,它就会返回我的票证数据和嵌入在该模型中的所有响应?
  • @jdavis89,使用来自 REST 或 JS 的包含过滤器。如果您发布您尝试过的内容,我可以提供更多指导。 docs.strongloop.com/display/public/LB/Include+filter
  • 在我的回答中添加了一些关于如何在查询中包含相关数据的详细信息。
  • 非常感谢您的帮助!可以使用 hasMany 关系和 include 过滤器。
【解决方案2】:

我听说您可以将嵌入式关系与 SQL 数据源一起使用,但数据随后会以字符串化 JSON 格式存储。

我已向https://docs.strongloop.com/display/LB/Embedded+models+and+relations 添加了一条注释。

兰德

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-30
    • 2021-11-19
    • 2019-04-25
    • 2013-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多