0

numeric order by child table field

This is not about actual families, but just illustrates the structure of the problem I am dealing with and my own actual tables and fields do not obscure the issue.

I have a parent and a child tables and parents have 0 or 1 child. I'm trying to get a list of parents who have a child and sorted by the child.Id field . So with 'children' being the relationship field FROM the child table:-

select parent where children order by children.Id

 

 

Easy enough and at first all looked good, but later I realised not so good when the number of parents with a child > 9.

You might guess where this is going.

I'm currently working with 12 such parents and the sort order I get is 1,10,11,12,2,3 etc … So the sorting is being done alphabetically on the child's Id field. Apart from thinking this being someone bizarre as the Id field is never text, I cannot seem to get over this problem.

I've tried children.number(Id) and number(children.Id) and then throwing a first() in there in case the possibility of multiples (although none) is causing the problem, but nothing makes any difference.

I know the 'order by' clause is being enacted as if I take it out, the order is quite different, probably based on the parent.Id. So it IS ordering the selected records, but I cannot prevent is treating children.Id (i.e. the child table's Id) as text.

The word 'bug' rears its ugly head, but is anyone able to select records sorted (numerically) by the Id of the child table?

9 replies

null
    • UKenGB
    • yesterday
    • Reported - view

    Further testing reveals that setting a formula field in the parent table equal to number(children.Id) and then sorting on that formula field results in an alpha sort, but sorting on number(formula) produces the correct numeric sort.

    Ok, so to get a numeric sort, you HAVE to use number() even when the field is already a number, but that only works for a field in the table being selected, i.e. the parent table in this case. Using the same number() coercion on a number field (not just Id) from the child table results in an alpha sort.

    This is wrong. Using 'order by' number(field) should ALWAYS produce a numeric sort, no matter where that field is.

    This is preventing me from doing what I need to do. I HAVE to be able to list those parent records ordered NUMERICALLY by the child.Id. The order is all important.

    Don't suppose anyone from Ninox bothers to read these forums nowadays. ☹️

    • UKenGB
    • yesterday
    • Reported - view

    I got around the problem by selecting on the child table, ordered by its own number(Id) which produces the correct numeric sort. 🙂

    However, that doesn't take away from the fact that it should be possible to select on a parent table and order numerically by a child.number(field). Ninox is doing that wrong.

    • UKenGB
    • yesterday
    • Reported - view

    Should of course have been "someWHAT bizarre…", but not possible to edit a post. 😡

      • UKenGB
      • yesterday
      • Reported - view

      That last post was in reply to and to correct the first post but which for some incomprehensible reason was placed at the end of the thread and no idea why I cannot edit any posts as I already did edit the first one, but which has now inexplicably stuck 'Copy' and a bunch of blank lines in the middle.

      So Ninox forums working as well as their 'order by' clause.

      Anyone reading this, make what sense of it you can. I give up.

    • Fred
    • yesterday
    • Reported - view

    It is confusing. Id would seem to be just a number but it actually contains the table code and the record number. If you do:

    debugValueInfo((select parent).children.Id)

    You will see an array of letters and numbers.

    That is why you need to wrap the Id with numbers if you just want numbers.

    • Alain_Fontaine
    • 11 hrs ago
    • Reported - view

    I tried to reproduce the problem. In the toy database in annex, the dashboard (Page1) contains two formulas, one without number() and one with it. The second one seems to give the intended ordering. As Fred mentioned, "Id", while usually displayed as a number, is not a number, but a "nid", which is a different type.

    • UKenGB
    • 10 hrs ago
    • Reported - view

    Thanks guys, I do understand that Id is not truly a number field, but in this context it would seem to make sense that it were treated as a number. However…

    That aside, as I pointed out, irrespective of what Id actually is, although number(Id) should produce a numerically sorted result, it does not when the Id is in a child table. As I discovered, it does when it is the Id of the table which is the subject of the 'select', but when it's from a child table the result is an alpha sort order.

    I struggled with this for some hours before realising it just would not work and switched to selecting from the child table, ordered by its own number(Id) and although producing the exact same data, the resulting list was numerically sorted as required.

    This was completely repeatable and I tried with other numeric fields, not the Id. Well, they were formula fields set to number(Id) which should mean they are numeric data, but in every case, if selecting the parent and 'order by' the child.field or even child.number(field), the results are alpha sorted, but if the select is on the child table, either of those 'order by' statements (i.e. using its own field or number(field) )  produce a numeric sort.

    This is the Mac app in MaOS 26. Maybe the cloud version is different. Wouldn't be the first time.

    • UKenGB
    • 10 hrs ago
    • Reported - view

    Thinking further, the child.Id will be an array since there could be many child records pointing to the same parent. In my case there were not, as additional code ensures it is a 1 to 1, but in the context of this selection, Ninox doesn't know that.

    So trying to numerically sort by the child.Id is actually trying to sort by an array and even child.number(Id) is then probably not truly a number. This situation does not arise when the select is on the child table and 'order by' on its own single Id, no array in the mix.

    This did occur to me before, hence why I tried to throw a first() in there, but maybe I didn't try hard enough. Maybe there is a way to do it with the correct combination of number() and first() and I just moved on before finding that particular combination. I may have only tried first(number()), whereas number(first()) would probably be the right way.

    Or maybe there is some inherent reason why Ninox cannot deal with coercing an array of child.Ids to a number. However, it does seem to me that this difference between the 2 methods is the most likely cause of the problem.

    As I said though, by selecting on the child table it does all work perfectly and in truth is probably the better way to perform the select, whatever the ordering required, so am happy to keep it like that and try to remember this potential issue so as not to waste time on it if it ever crops up again.

    • Alain_Fontaine
    • 8 hrs ago
    • Reported - view

    Indeed, an array of whatever cannot be coerced into a single number. An element must first be singled out by applying a suitable function. So the database I sent this morning is wrong, and only seems to deliver to intended result by pure luck (explanation below if anyone cares). So both "first" and "number" must be applied, either as number(first(Children).Id) or number(first(Children.Id)).

    Why the intended result seemed to be returned: trying to coerce an array into a single number returns nothing at all. When sorting on nothing, the records are ordered in their "natural" order, which is by… Id. The test data where such that this happened to be the same as the intended result.

Content aside

  • 8 hrs agoLast active
  • 9Replies
  • 17Views
  • 3 Following