【问题标题】:Sails.js one to many association with postgreSQL: column does not existSails.js 与 postgreSQL 的一对多关联:列不存在
【发布时间】:2017-08-06 10:34:41
【问题描述】:

在sails 0.12.13 与postgresql 的关联方面,我需要一些帮助。 我有一个“App”模型和一个“Membership”模型。关系应该是一对多的(一个应用程序可以与许多关系相关联)。 这是 App 模型 db 表架构(表称为“apps”):

Table "public.apps"
   Column   |            Type             |                     Modifiers                     
------------+-----------------------------+---------------------------------------------------
 id         | integer                     | not null default nextval('apps_id_seq'::regclass)
 name       | character varying           | not null
Indexes:
    "apps_pkey" PRIMARY KEY, btree (id)
    "apps_name_key" UNIQUE CONSTRAINT, btree (name)
Referenced by:
    TABLE "memberships" CONSTRAINT "app_fk" FOREIGN KEY (app_id) REFERENCES apps(id) ON UPDATE RESTRICT ON DELETE CASCADE

这是会员资格:

Table "public.memberships"
   Column   |            Type             |                        Modifiers                         
------------+-----------------------------+----------------------------------------------------------
 id         | integer                     | not null default nextval('memberships_id_seq'::regclass)
 app_id     | integer                     | not null
Foreign-key constraints:
    "app_fk" FOREIGN KEY (app_id) REFERENCES apps(id) ON UPDATE RESTRICT ON DELETE CASCADE

在我的用户模型中,我有这个:

module.exports = {
  tableName: 'apps',
  autoCreatedAt: false,
  autoUpdatedAt: false,

  attributes: {
    name: { type: 'string', unique: true, required: true, alphanumericdashed: true },
    memberships: { collection: 'memberships', model: 'Membership' },
 }
}

这是会员模型:

module.exports = {
  tableName: 'memberships',
  autoCreatedAt: false,
  autoUpdatedAt: false,
  attributes: {
    app: { model: 'app', columnName: 'app_id' },
  },
};

当我尝试查询应用并获取其成员资格时:

App.find({ id: 1 }).populate('memberships').exec((err, app) => {
      if (err) throw err;

      console.log(app.memberships);
    });

我收到此错误:

Error (E_UNKNOWN) :: Encountered an unexpected error
error: column apps.memberships does not exist
    at Connection.parseE (/usr/src/app/node_modules/sails-postgresql/node_modules/pg/lib/connection.js:539:11)
    at Connection.parseMessage (/usr/src/app/node_modules/sails-postgresql/node_modules/pg/lib/connection.js:366:17)
    at Socket.<anonymous> (/usr/src/app/node_modules/sails-postgresql/node_modules/pg/lib/connection.js:105:22)
    at emitOne (events.js:115:13)
    at Socket.emit (events.js:210:7)
    at addChunk (_stream_readable.js:252:12)
    at readableAddChunk (_stream_readable.js:239:11)
     at Socket.Readable.push (_stream_readable.js:197:10)
     at TCP.onread (net.js:589:20)

看起来关联未“启用”,并且 waterline 正在我的模型中搜索实际列“成员资格”。谁能解释我做错了什么?谢谢

【问题讨论】:

    标签: sails.js waterline sails-postgresql


    【解决方案1】:

    根据documentation,我猜你的联想不好。

    // App.js
    module.exports = {
      tableName: 'apps',
      autoCreatedAt: false,
      autoUpdatedAt: false,
    
      attributes: {
        name: {
          type: 'string',
          unique: true,
          required: true,
          alphanumericdashed: true
        },
        memberships: {
          collection: 'membership', // <-- changed to singular (as your model should be)
          via: 'app'                // <-- use "via" instead of "model"
        },
      }
    }
    
    // Membership.js
    module.exports = {
      tableName: 'memberships',
      autoCreatedAt: false,
      autoUpdatedAt: false,
      attributes: {
        app: {
          model: 'app'  
          // <-- removed the "columnName" here
        },
      },
    };

    此外,惯例通常将您的模型命名为单个实例。例如,它的“User.js”而不是“Users.js”。将 collection 称为复数形式是有效的。我对您的命名进行了一些更改,但您必须看看这会如何影响您的文件(因为您没有提供这些名称)。

    【讨论】:

    • 就是这样。谢谢。顺便说一句,我的模型文件被命名为“App.js”和“Membership.js”,单数。
    • 实际上,@vol7ron,错误消失了,但我仍然没有得到我想要的数据。事实上,当我尝试获取一个应用程序的成员资格时,执行的查询是这样的: SELECT * FROM "public"."memberships" AS "memberships" WHERE "app" = 1 ORDER BY "memberships"."id" ASC as你可以看到,关系引用了错误的列名“app”(正确的列名是“app_id”。你知道如何改变它吗?
    • 如果您正在尝试查找 app 1 的成员资格,该查询看起来是正确的
    • 知道了,我需要 Membership.js 的“app”属性中的“columnName”
    • @whites11 继续编辑此答案。我相信其他人(可能是我)将来可能会提到它。我是否正确地假设,您添加了 columnName 回到原来的状态?我想我认为糟糕的语法实际上是有效的——那太好了!
    猜你喜欢
    • 2015-06-27
    • 2016-12-09
    • 1970-01-01
    • 1970-01-01
    • 2014-12-01
    • 1970-01-01
    • 2015-06-01
    • 1970-01-01
    • 2015-02-01
    相关资源
    最近更新 更多