Question

Database Diagram

I have a database structure like this. Every employee belongs to a department, every stock belongs to a department, every department has a list of employees and list of stocks associated with it. I initialize the database with this code:

$data.define("Org.Department",{
  ID: {key: true, type: "integer", computed: true},
  DepartmentID: {type: "integer", required: true},
  DepartmentName: {type: "string", required: true},
  Employee:{type: Array, elementType: "Org.Employee", navigationProperty:"Department"},
  Stock:{type: Array, elementType: "Org.Stock", navigationProperty:"Department"}
});

$data.define("Org.Employee",{
  ID: {key: true, type: "integer", computed: true},
  Name: {type: "string", required: true},
  DepartmentID: {type: "integer", required: true},
  Department: {type: "Org.Department", inverseProperty: "Employee"}
});

$data.define("Org.Stock",{
  ID: {key: true, type: "integer", computed: true},
  DepartmentID: {type: "integer", required: true},
  Description: {type: "string"},
  NumItems: {type: "integer", required: true},
  Department: {type: "Org.Department", inverseProperty: "Stock"}
});

$data.EntityContext.extend("Company",{
  Employees: {type: $data.EntitySet, elementType: Org.Employee},
  Departments: {type: $data.EntitySet, elementType: Org.Department},
  Stocks: {type: $data.EntitySet, elementType: Org.Stock}
});

var x= new Company({
  provider: "webSql",
  databaseName: "DB",
  version: 1,
  dbCreation: $data.storageProviders.DbCreationType.DropAllExistingTables
});

What i want to do is adding elements to that database but I have to make the proper connections. By I mean proper connections, I want to query name of all employees whose is which requires me to look up the department name and take the list of employees from it and then listing their names. I tried many things but it utterly fails. For example:

x.onReady(function(){
  var dep;
  x.Departments.filter(function(res){
    return res.DepartmentID==4;
  }).forEach(function(it){
    dep=it;
  });
  console.log(dep);
    var emp=new Org.Employee({
    Name: 'Employee1',
    DepartmentID: 4,
    Department:dep
  });
  x.Employees.add(emp);
  x.Employees.saveChanges();
});

Which fails to insert the proper Department information inside the employee. When i query the Department field of the Employee1 I get undefined.

This also fails:

x.Employees.add({Name:"Employee1",DepartmentID:4});

All the queries successfully inserts some data to the database but their Department field is empty which defeats the whole purpose of having a relational database.

Was it helpful?

Solution

I fixed two important things in the code and now it's working:

  1. navigationProperty should be inverseProperty. You always have to define the other side of the relationship with inverseProperty - please share the URL where you had found this navigationProperty, I check if I can fix it
  2. if you read an item from the DB and set it in a relationship, you have to attach the item to the context

    $data.define("Org.Department",{ ID: {key: true, type: "int", computed: true}, DepartmentID: {type: "int", required: true}, DepartmentName: {type: "string", required: true}, Employee:{type: Array, elementType: "Org.Employee", inverseProperty: "Department"}, Stock:{type: Array, elementType: "Org.Stock", inverseProperty: "Department"} });

The fixed code:

$data.define("Org.Employee",{
  ID: {key: true, type: "int", computed: true},
  Name: {type: "string", required: true},
  DepartmentID: {type: "int", required: true},
  Department: {type: "Org.Department", inverseProperty: "Employee"}
});

$data.define("Org.Stock",{
  ID: {key: true, type: "int", computed: true},
  DepartmentID: {type: "int", required: true},
  Description: {type: "string"},
  NumItems: {type: "int", required: true},
  Department: {type: "Org.Department", inverseProperty: "Stock"}
});

$data.EntityContext.extend("Company",{
  Employees: {type: $data.EntitySet, elementType: Org.Employee},
  Departments: {type: $data.EntitySet, elementType: Org.Department},
  Stocks: {type: $data.EntitySet, elementType: Org.Stock}
});

var x= new Company({
  provider: "webSql",
  databaseName: "DB",
  version: 1
});

x.onReady(function(){
    var newDep = new x.Departments.elementType({DepartmentName: 'dep1', DepartmentID: 7});
    x.Departments.add(newDep);
    x.saveChanges(function() {
        x.Departments.first("it.DepartmentName == 'dep1'", {}, function(dep){
              console.log(dep);
              x.Departments.attach(dep);
              var emp = new Org.Employee({
                Name: 'Employee1',
                DepartmentID: dep.DepartmentID,
                Department:dep
              });
              console.log(emp);
              x.Employees.add(emp);
              x.Employees.saveChanges();
        });
    })
});

You can find a the live version on jsfiddle

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top