# Algebra clause issue, possible bug?

**URL:** <https://forums.bitovi.com/t/algebra-clause-issue-possible-bug/682>\
**Category:** CanJS\
**Created:** [July 9, 2017, 10:08pm UTC](https://forums.bitovi.com/t/algebra-clause-issue-possible-bug/682 "2017-07-09T22:08:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gregorio\_Godoy](https://avatars.discourse-cdn.com/v4/letter/g/f1d935/32.png) [@Gregorio\_Godoy](https://forums.bitovi.com/u/Gregorio_Godoy)\
**Post date:** [July 9, 2017, 10:08pm UTC](https://forums.bitovi.com/t/algebra-clause-issue-possible-bug/682/1 "2017-07-09T22:08:12Z")

</div>

Hi all!

After 4 days scratching my head i found an issue with algebra and compound id’s. I will explain it with example. Assuming i have a model list like this:

```auto
[
	{
		"invoice_number": 5,
		"customer_id": 2,
		"date": "2017-07-05",
		"total": 120,
		"invoice_detail": [
			{
				"invoice_number": 5,
				"product_code": 23,
				"quantity": 1,
				"prize": 62,
				"subtotal": 62
			},
			{
				"invoice_number": 5,
				"product_code": 13,
				"quantity": 1,
				"prize": 58,
				"subtotal": 58
			}
		]

	},
	{
		"invoice_number": 6,
		"customer_id": 2,
		"date": "2017-07-05",
		"total": 80,
		"invoice_detail": [
			{
				"invoice_number": 6,
				"product_code": 47,
				"quantity": 2,
				"prize": 40,
				"subtotal": 80
			}
		]
	}
]

```

I have two tables: invoice and invoice\_detail. Here the maps (Im using the superMap) :

invoice\_detail

```auto
const InvoiceDetail = DefineMap.extend({
	seal: false
}, {
	invoice_number: 'number',
	product_code: 'number',
	...
});

const algebra = new set.Algebra(
	set.props.id('invoice_number'),
	set.props.id('product_code')
);

InvoiceDetail.List = DefineList.extend({
	'#': InvoiceDetail
});

```

Invoice

```auto
import InvoiceDetail from 'baker/models/invoice_detail';
const Invoice = DefineMap.extend({
	seal: false
}, {
	invoice_number: 'number',
	...
	invoice_detail: {
		Type: InvoiceDetail.List,
		set: function(newVal) {
			if (newVal) {
				InvoiceDetail.Connection.addListReference(newVal,{
					invoice_number: this.invoice_number
				});
            }
			
			return newVal;
		}
	}
});

const algebra = new set.Algebra(
	set.props.id('invoice_number')
);

Invoice.List = DefineList.extend({
	'#': Invoice
});
...

```

As you can see, invoice’s id is invoice\_number and invoice\_detail’s id is invoice\_number and product\_code (natural compound id).

So, when i retrieve an invoice with getList({}) the API returns all invoices, with all they details. For each invoice\_detail im adding a list reference using the current invoice\_number as set.

```auto
invoice_detail: {
		Type: InvoiceDetail.List,
		set: function(newVal) {
			if (newVal) {
				InvoiceDetail.Connection.addListReference(newVal,{
					invoice_number: this.invoice_number
				});
            }
			
			return newVal;
		}
	}

```

So far no problem. But when im creating a new invoice detail and persisting it to API, the callback is adding this invoice\_detail to all invoices list!! I tracked the problem and i think i found it. The update function from the real-time behavior goes through each list and sees if the list should be updated with the new created instance. Basically what it does is checking if the algebra has the set in they properties:

Line 398  
if(canSet.has(set, props, self.algebra)) {

> <https://github.com/canjs/can-connect/blob/master/real-time/real-time.js>

The `has` function is in the set-core, and starts in the line 494

> <https://github.com/canjs/can-set/blob/master/src/set-core.js>

Inside the function, in the line 516

```auto
result = this.evaluateOperator(compare.subset, props, set, {isProperties: true}, undefined);

```

That brings us to the getClausePorperties called inside evaluateOperator function:

```auto
var aClauseProps = this.getClauseProperties(a, aOptions),
			bClauseProps = this.getClauseProperties(b, bOptions),
			set = {},
			useSet;

```

Lets look closer to `getClauseProperties`. It loops trough all non-where clauses first (order, paginate, id) and looks for a property match in the set. If its founded, its assigned to the clause properties and DELETED from the cloned set. All other properties are assigned to the where clause.

The problem is that the set looks like `{ invoice_number: 5 }` and as it loops first trough all non-where clauses, assigned to the id clauses and deleted, the where clause is empty. That is the reason why the created invoice\_detail is added to all invoices list, because the where clause is empty.

I can understand the logic. Usually if the set property is the id property it can’t be the where clause. Following this logic, if a set property belongs to the id, order or paginate it cannot be part of the where clause. And thats absolutely logic if all id’s are just a single property. But in this case the id is compound: invoice\_number, product\_code. So, when i pass the invoice\_number as a set in the invoice detail list, it should be added to the where clause and not to the id clause.

I think this is a bug, because `getClauseProperties` should evaluate if the id is really set, in this case invoice\_number, product\_code. Both must be in the set to assign it in the id clause. If not all id properties are set, they should be assign to the where clause.

Is this really a bug or did i misunderstood something? THANKS in advance!!

---

<div class="post-metadata">

**Author:** ![justinbmeyer](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.bitovi.com/justinbmeyer/32/179_2.png) [@justinbmeyer](https://forums.bitovi.com/u/justinbmeyer)\
**Post date:** [July 10, 2017, 12:21am UTC](https://forums.bitovi.com/t/algebra-clause-issue-possible-bug/682/2 "2017-07-10T00:21:17Z")

</div>

Almost certainly a bug. Good catch! Can you create an issue? Thanks!

---

<div class="post-metadata">

**Author:** ![Gregorio\_Godoy](https://avatars.discourse-cdn.com/v4/letter/g/f1d935/32.png) [@Gregorio\_Godoy](https://forums.bitovi.com/u/Gregorio_Godoy)\
**Post date:** [July 10, 2017, 2:21am UTC](https://forums.bitovi.com/t/algebra-clause-issue-possible-bug/682/3 "2017-07-10T02:21:40Z")

</div>

Sure! Never before opened an issue, hope this is ok:

> <https://github.com/canjs/canjs/issues/3409>
>
> In the can-set the getClauseProperties function, breakup incorrectly the id and where clause when the algebra has multiple ids.
> A full explanation...

Sorry for the short description and content in the opened issue. Im going to sleep 😴
