Skip to content

lb3 - Relations on uuid #4287

@fasenderos

Description

@fasenderos

Steps to reproduce

When the foreignKey of the relations point to ID everything works as expected, while if I try to point the foreignKey to UUID, the UUID is parsed as integer (NaN) and the relation not work.

To reproduce create 2 models:

Customer

{
  "name": "customer",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "id": {
      "type": "number"
    },
    "uuid": {
      "type": "string",
      "defaultFn": "uuidv4"
    },
    "name": {
      "type": "string"
    }
  },
  "validations": [],
  "relations": {
    "addresses": {
      "model": "address",
      "type": "hasMany",
      "foreignKey": "customerId"
    }
  },
  "acls": [],
  "methods": {}
}

Address

{
  "name": "address",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "id": {
      "type": "number"
    },
    "uuid": {
      "type": "string",
      "defaultFn": "uuidv4"
    },
    "customerId": {
      "type": "number",
      "mysql": {
        "columnName": "customer_id"
      }
    },
    "customerUuid": {
      "type": "string",
      "mysql": {
        "columnName": "customer_uuid"
      }
    },
    "street": {
      "type": "string"
    }
  },
  "validations": [],
  "relations": {
    "customer": {
      "model": "customer",
      "type": "belongsTo",
      "foreignKey": "customerId"
    }
  },
  "acls": [],
  "methods": {}
}

Get customer with address related by ID:

const customer = await app.models.Customer.findById(1, {  include: { relation: 'addresses' } });
console.log(customer);

work

Now in Address replace "foreignKey": "customerId" with "foreignKey": "customerUuid" and you get customerUuid as NaN:
nan

If you do the same thing for Customer model by replacing the "foreignKey": "customerId" with "foreignKey": "customerUuid", you get an empty addresses list
empty

Expected Behavior

The ability to use UUID on to relate model

Additional information

Node: v10.14.1
Npm: v6.4.1
Database: 10.1.30-MariaDB

lb --version
5.0.3 (generator-loopback@6.1.1 loopback-workspace@5.0.0)

"dependencies": {
    "compression": "^1.0.3",
    "cors": "^2.5.2",
    "helmet": "^3.10.0",
    "loopback": "^3.22.0",
    "loopback-boot": "^2.6.5",
    "loopback-component-explorer": "^6.2.0",
    "loopback-connector-mysql": "^5.4.2",
    "serve-favicon": "^2.0.1",
    "strong-error-handler": "^3.0.0"
  }

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions