Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

way to model a one-to-one relationship in schema?

Tags:

json

backand

For example I have a table of cars, each has a model and a make. A car can only have one of each, and there is a table of models and makes, with their ids auto-generated. Can I reference these in my cars table? What would the JSON look like?

I tried to create a new field in the cars table and specify id in the "Via" text box, but it just creates a new column in the corresponding table, id1. Is there a proper way?

Here is my sample schema JSON:

  {
    "name": "cars",
    "fields": {
      "model": {
        "object": "models"
      },
      "make": {
        "object": "makes"
      }
    }
  },
  {
    "name": "models",
    "fields": {
      "title": {
        "type": "string"
      },
      "make": {
        "object": "makes"
      },
      "id": {
        "collection": "cars",
        "via": "model"
      }
    }
  },
  {
    "name": "makes",
    "fields": {
      "models": {
        "collection": "models",
        "via": "make"
      },
      "title": {
        "type": "string"
      },
      "id": {
        "collection": "cars",
        "via": "make"
      }
    }
  }
like image 778
montrealist Avatar asked Oct 18 '22 10:10

montrealist


1 Answers

In Back& there is no one-to-one relationship by definition but we you have looking for is one-to-many and you almost got it, see the below Model that works. In the below model a car can have single model and single make (of course a makes and models can have many cars - this is the many part):

{
  "name": "cars",
  "fields": {
    "model": {
      "object": "models"
    },
    "make": {
      "object": "makes"
    }
  }
},
{
  "name": "models",
  "fields": {
    "cars": {
      "collection": "cars",
      "via": "model"
    },
    "title": {
      "type": "string"
    },
    "make": {
      "object": "makes"
    }
  }
},
{
  "name": "makes",
  "fields": {
    "cars": {
      "collection": "cars",
      "via": "make"
    },
    "models": {
      "collection": "models",
      "via": "make"
    },
    "title": {
      "type": "string"
    }
  }
}
like image 151
Itay Avatar answered Nov 15 '22 07:11

Itay