环回 3 "hasOne" 关系

Loopback 3 "hasOne" relation

我有 2 个模型

商家模型

{
  "name": "Merchant",
  "plural": "Merchants",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true,
    "mysql": {
      "table": "tbl_merchants"
    }
  },
  "properties": {
    "merchant_id": {
      "type": "string",
      "id": true,
      "defaultFn": "uuidv4"
    },
    "owner_id": {
      "type": "string",
      "id": true,
      "length": 36
    },
    "merchant_name": {
      "type": "string",
      "required": true,
      "length": 100
    },
    "address": {
      "type": "string"
    },
    "phone_number": {
      "type": "string",
      "length": 50
    },
    "email": {
      "type": "string",
      "required": true,
      "length": 100
    },
    "status": {
      "type": "number",
      "length": 1
    },
    "notify_queue_no": {
      "type": "number",
      "length": 1
    },
    "subscription_status": {
      "type": "number",
      "length": 1
    },
    "merchant_category_id": {
      "type": "string",
      "length": 36
    },
    "merchant_type_id": {
      "type": "string",
      "length": 36
    },
    "merchant_link": {
      "type": "string",
      "length": 100
    },
    "owner_id": {
      "type": "string",
      "length": 36
    }
  },
  "validations": [],
  "relations": {
    "user": {
      "type": "hasOne",
      "model": "UserData",
      "foreignKey": "user_id",
      "primaryKey": "owner_id"
    },
    "items": {
      "type": "hasMany",
      "model": "Item",
      "foreignKey": "merchant_id"
    },
    "item_categories": {
      "type": "hasMany",
      "model": "ItemCategory",
      "foreignKey": "merchant_id"
    }
  },
  "acls": [
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "DENY"
    },
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "$authenticated",
      "permission": "ALLOW"
    },
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "DENY"
    },
    {
      "accessType": "READ",
      "principalType": "ROLE",
      "principalId": "merchantOwner",
      "permission": "ALLOW",
      "property": "findById"
    },
    {
      "accessType": "READ",
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "ALLOW",
      "property": "count"
    }
  ],
  "methods": {}
}

商家只有一个用户 这是我的用户模型

{
  "name": "UserData",
  "base": "User",
  "public": false,
  "options": {
    "mysql": {
      "table": "tbl_users"
    }
  },
  "properties": {
    "user_id": {
      "type": "string",
      "id": true,
      "length": 36,
      "defaultFn": "uuidv4",
      "dataType": "char"
    },
    "email": {
      "type": "string"
    }
  },
  "validations": [],
  "relations": {},
  "acls": [
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "DENY"
    },
    {
      "accessType": "EXECUTE",
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "ALLOW",
      "property": "loginApp"
    }
  ],
  "methods": {}
}

我想创建一个远程方法来添加商家和该商家的所有者。这是我当前的远程方法:

'use strict';
var app = require('../../server/server');
module.exports = function(Obj) {
 Obj.createMerchant = function(req, cb) {
  //create the merchant
  Obj.beginTransaction({
   isolationLevel: Obj.Transaction.READ_COMMITTED
  }, function(err, tx) {
   Obj.create([{
    "merchant_name": req.merchant_name,
    "email": req.merchant_email
   }], {transaction: tx}, function(err, merchant){
    if (err){
     tx.rollback();
     return cb(err, null);
    } else {
     Obj.prototype.user.create({
      "merchant_id": merchant.merchant_id,
      "email": req.email,
         "password": req.password
     }, {transaction: tx}, function(err, user) {
      if (err) {
       tx.rollback();
       return cb(err, null);
      } else {
       tx.commit(function(err) {
        if (err){
              return cb(err, null);
             }

             return cb(null, merchant);
             });
      }
        })
    }
   })
  });
 };

 Obj.remoteMethod('createMerchant', {
  description: "Create merchant and it's owner",
     accepts: [
         {arg: 'req', type: 'object', http: { source: 'body' }}
       ],
     returns: {arg: 'list', type: 'object'},
     http: {path:'/createMerchant', verb: 'post'}
   });
};

远程方法return这个错误:

{
  "error": {
    "statusCode": 500,
    "name": "Error",
    "message": "HasOne relation cannot create more than one instance of UserData"
  }
}

知道如何解决这个问题吗?非常感谢

总之关系部分有逻辑错误。关系应该是 "belongsTo" 而不是 "hasOne",这是因为 hasOne 将在父模型中创建或 select id。我会关闭这个问题。