Reference fields (relationships) not working

I have the same problem. But I managed to get relationships by posting JSON schema of my entire database at once (by using createTables() in API Docs)
First, You have to create relationships with foreign keys pointing to primary keys of Your reference table (unless You know how to create unique keys). Use this example as a Reference:

{
      "name": "ThornUsers",
      "label": "ThornUsers",
      "plural": "ThornUsers",
      "primary_key": "Id",
      "name_field": null,
      "field": [
        {
          "name": "Id",
          "label": "Id",
          "type": "id",
          "db_type": "int(11)",
          "length": 11,
          "precision": 11,
          "scale": 0,
          "default": null,
          "required": false,
          "allow_null": false,
          "fixed_length": false,
          "supports_multibyte": false,
          "auto_increment": true,
          "is_primary_key": true,
          "is_foreign_key": false,
          "ref_table": "",
          "ref_fields": "",
          "validation": null,
          "value": []
        },
        {
          "name": "Username",
          "label": "Username",
          "type": "string",
          "db_type": "varchar(255)",
          "length": 255,
          "precision": 255,
          "scale": 0,
          "default": null,
          "required": true,
          "allow_null": false,
          "fixed_length": false,
          "supports_multibyte": true,
          "auto_increment": false,
          "is_primary_key": false,
          "is_foreign_key": false,
          "ref_table": "",
          "ref_fields": "",
          "validation": null,
          "value": []
        }
      ],
      "related": [
        {
          "name": "Leases_by_ThornUserId",
          "type": "has_many",
          "ref_table": "Leases",
          "ref_field": "ThornUserId",
          "field": "Id"
        },
        {
          "name": "Rentables_by_Leases",
          "type": "many_many",
          "ref_table": "Rentables",
          "ref_field": "Id",
          "join": "Leases(ThornUserId,RentableId)",
          "field": "Id"
        },
        {
          "name": "Rentables_by_OwnerId",
          "type": "has_many",
          "ref_table": "Rentables",
          "ref_field": "OwnerId",
          "field": "Id"
        }
      ],
      "access": [
        "GET",
        "POST",
        "PUT",
        "PATCH",
        "MERGE",
        "DELETE"
      ]
    },
    {
      "name": "Leases",
      "label": "Leases",
      "plural": "Leases",
      "primary_key": "RentableId",
      "name_field": null,
      "field": [
        {
          "name": "RentableId",
          "label": "RentableId",
          "type": "reference",
          "db_type": "int(11)",
          "length": 11,
          "precision": 11,
          "scale": 0,
          "default": null,
          "required": true,
          "allow_null": false,
          "fixed_length": false,
          "supports_multibyte": false,
          "auto_increment": false,
          "is_primary_key": true,
          "is_foreign_key": true,
          "ref_table": "Rentables",
          "ref_fields": "Id",
          "validation": null,
          "value": []
        },
        {
          "name": "ThornUserId",
          "label": "ThornUserId",
          "type": "reference",
          "db_type": "int(11)",
          "length": 11,
          "precision": 11,
          "scale": 0,
          "default": null,
          "required": true,
          "allow_null": false,
          "fixed_length": false,
          "supports_multibyte": false,
          "auto_increment": false,
          "is_primary_key": false,
          "is_foreign_key": true,
          "ref_table": "ThornUsers",
          "ref_fields": "Id",
          "validation": null,
          "value": []
        },
        {
          "name": "StartTimestamp",
          "label": "StartOfLease",
          "type": "timestamp_on_create",
          "db_type": "timestamp",
          "length": 0,
          "precision": 0,
          "scale": 0,
          "default": "0000-00-00 00:00:00",
          "required": false,
          "allow_null": false,
          "fixed_length": false,
          "supports_multibyte": false,
          "auto_increment": false,
          "is_primary_key": false,
          "is_foreign_key": false,
          "ref_table": "",
          "ref_fields": "",
          "validation": null,
          "value": []
        },
        {
          "name": "Ongoing",
          "label": "OngoingLease",
          "type": "boolean",
          "db_type": "tinyint(1)",
          "length": 1,
          "precision": 1,
          "scale": 0,
          "default": 1,
          "required": false,
          "allow_null": true,
          "fixed_length": false,
          "supports_multibyte": false,
          "auto_increment": false,
          "is_primary_key": false,
          "is_foreign_key": false,
          "ref_table": "",
          "ref_fields": "",
          "validation": null,
          "value": []
        },
        {
          "name": "EndTimestamp",
          "label": "EndOfLease",
          "type": "timestamp",
          "db_type": "timestamp",
          "length": 0,
          "precision": 0,
          "scale": 0,
          "default": null,
          "required": false,
          "allow_null": true,
          "fixed_length": false,
          "supports_multibyte": false,
          "auto_increment": false,
          "is_primary_key": false,
          "is_foreign_key": false,
          "ref_table": "",
          "ref_fields": "",
          "validation": null,
          "value": []
        }
      ],
      "related": [
        {
          "name": "Rentables_by_RentableId",
          "type": "belongs_to",
          "ref_table": "Rentables",
          "ref_field": "Id",
          "field": "RentableId"
        },
        {
          "name": "ThornUsers_by_ThornUserId",
          "type": "belongs_to",
          "ref_table": "ThornUsers",
          "ref_field": "Id",
          "field": "ThornUserId"
        }
      ],
      "access": [
        "GET",
        "POST",
        "PUT",
        "PATCH",
        "MERGE",
        "DELETE"
      ]
    }

You have every possible relationship (belongs_to, has_many, many_many) declared in this example, so just fork it to suit Your needs.

1 Like