project-imas/encrypted-core-data — pull request #112 by rhodgkins (2014-08-31 18:22:04+00:00)
Title: Relationships do not create the correct sub-entity

**N.B.** This pull request does fix an issue, but there are still other issues

I'm going to try to explain this but it might be better having a look at the object graph and test cases!

![Object graph](http://i61.tinypic.com/2hqqd1k.png)

`Root` allows sub entities of `Parent` to be stored against it, but the `Parent` entity description of any subentites is returned instead of the specific `ChildA` or `ChildB` entity descriptions.

730554f adds 2 failing test cases for one-to-one and many-to-many relationships.

- [x] **one-to-many relationships** (fixed in 834de26)
This was fixed in the same way as when fetching entities based on parent entities, looking up the hash of the entity name from `entityTypeCache` (line [231](https://github.com/project-imas/encrypted-core-data/blob/master/Incremental%20Store/EncryptedStore.m#L231) and [267](https://github.com/project-imas/encrypted-core-data/blob/master/Incremental%20Store/EncryptedStore.m#L267)).
I think this also needs to be applied to the other 2 relational types.

- [ ] **one-to-one relationships**
I think these types of relationships with subentites need an extra column adding to the original table (where appropriate) so instead of:
```sql
CREATE TABLE ecdRoot ('__objectid' integer primary key, 'oneToOne__objectid', 'name');
```
Does another column for the type of the `oneToOne` entity type?
```sql
CREATE TABLE ecdRoot ('__objectid' integer primary key, 'oneToOne__objectid', 'Parent__entityType' integer, 'name');
```
- [ ] **many-to-many relationships**
This has deeper issues as the relation table name columns are based on the relational subentities not its parent entities. For example the following table is created for the above object graph:
```sql
CREATE TABLE ecd_manyToMany_manyToManyInverse ('ChildA__objectid' INTEGER NOT NULL, 'Root__objectid' INTEGER NOT NULL, PRIMARY KEY('ChildA__objectid', 'Root__objectid'))
```
But I think it the primary key column need to be based on the super entity and also include a column for the entity type (if applicable):
```sql
CREATE TABLE ecd_manyToMany_manyToManyInverse ('Parent__objectid' INTEGER NOT NULL, 'Parent__entityType' integer, 'Root__objectid' INTEGER NOT NULL, PRIMARY KEY('Parent__objectid', 'Root__objectid'))
```

I'm going to try looking at the other issues, but I think I'm along right lines with the last 2 issues?