Undefined Relations

I am consistently getting undefined relations in the debugger despite restarting the application. Please see attached images. As you can see DogstoVac is the relationship between the dogs table and the vaccinations table which is okay. The other two DogstoBreed and DogsToLitter are showing as undefined.

I have noticed that when naming relations in the structure editor if you don’t press tab the relation name does not seem to be accepted. There is a glitch somewhere. Version 18.2
Thanks
Paul

Snap2 Snap3

With the picture I can only guess usually cases.
The table Litter, has it the attribute “Publish as REST resource” enabled and has it a field marked as primary key (displayed underlined)?

Thanks for reply Thomas,

Yes PK always defined. I disabled rest resource on tables and also the map to Null on the fields used for relation. No difference.

Can you confirm that for both one to many and many to one relations the debugger should show zero selection for the relation if there are no matching records.

I can replicate the scenario where breed (one) table has a record then DogsToBreed shows as a valid relation with one selection. However if Dogs (Many) table does not have a one record in Breeds then the relationship shows as undefined.

The problem seems to be occurring where the relationship is from the many table to the one.
Thanks
Paul

Ah, that was the missing link.

When you have an undefined relation, then it is undefined, yes.
Undefined relation = orphan record.

An invoice item without an invoice
An invoice without customer.

A dog vaccination without a dog…

In that case, it is undefined. You need to assign a dog to the vaccination to make the relation valid/defined.

Thanks for confirming I was expecting to see 0 records rather than undefined. To clarify if I am using dot notation and the relation is undefined because a record does not exist in the many table this is still valid syntax and will not cause an error ?

For example if DogsToBreed was undefined as no record in breed table. This works but related to my other post.

COLLECTION TO ARRAY($puppylist;breedname_at;“DogsToBreed.Name”)

However this will cause an error

If ($element.DogsToBreed.Name#"")

But this works

If ($element.DogsToBreed.Name#Null)

Thanks

Actually when I was working though this I did discover that there should really be a definition. But when the item has no record it actually should be defined but with 0 records. At least when I was on with tech support and we were trying to figure out why my picture field keep coming up as undefined. If the relationship is defined then there should be something that comes up in the debugger. But as of now it does not.

Your workaround is the way I have been doing testing to see if there is a link. If the link comes back as null then there is no record there yet. I had challenged with that in my main list when I wanted to have a count of requests for my cases. I was looking for a count number in the list view of the requests column of my case summary list. The way it is currently written you cannot use something like getting a count() of the number of records that are related. Following a One to Many. If you have no records in the many for the one the undefined comes back in gives you an internal 4D error. Which prevents the count() function for getting a number of records related.

That results in having to write a bunch more checking code to make sure that undefined does not give you a column of errors.

0 records? This is the case if that is a one to many relation.
If I understood correctly, in your case it is a many to one relation.
So there is either a record - or no record. No is not the same as 0 records. No is NULL.

To demonstrate that, simple example with two tables (1 and 2) and a relation from 2 to 1, named “to_one_table”.

0 records are existing.

Code:

$ent:=ds.table_2.new()
If ($ent.to_one_table=Null)
	$ent2:=ds.table_1.new()
	$ent2.save()
	$ent.to_one_table:=$ent2
End if 
$ent.save()

As the record is new created, of course there is no related record.
I compare the relation with NULL. Is there are record (entity) or not?
If not, I create it - and assign it.

(Notice that you can assign an entity directly, no need to deal with Primary keys to do so, 4D knows which fields to assign).

Thanks Thomas, Ive got it.
Just a thought but the confusion is caused because when you do not have one a record the relation does not appear in the debugger despite been defined in the structure. It is the fact that it’s missing that confused me. Perhaps it would make sense to have the relation defined in the debugger but show Null for number of records. This would make it more obvious for the developer to remember to test for NULL.