bubleh
bubleh

Reputation: 378

Loopback 3 "hasOne" relation

i have 2 models

MerchantModel

{
  "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": {}
}

merchant has only one user this is my user model

{
  "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": {}
}

i want to create a remote method to add the merchant and the owner of that merchant. this is my current remote method:

'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'}
  	});
};

The remote method return this error:

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

Any idea how to solve this problem? thanks a lot

Upvotes: 0

Views: 732

Answers (1)

bubleh
bubleh

Reputation: 378

Anyway there is some logic error with the relation part. The relation should be "belongsTo" instead of "hasOne", it's because hasOne will create or select the id in the parent model. I will close this question.

Upvotes: 1

Related Questions