I noticed some inconsistency in the generated columns and I wonder if it’s a bug or I’ve done something wrong.
Basically I am trying to reference a property in a related object. In this test example, the table has a 1:1 relation to “agencies” table. The column is called “agency”.
I assume I can reference it by specifying agency.objectId and it seems to allow me to do so. (see attached)
But instead it’s actually just returning the objectId of the current table:
Is there a way I can achieve this?
My actual goal is as follows:
- I have a table called “agencies”
- agencies can create “clients”
- “clients” can have a reference that’s unique to the agency, but not unique to client table.
My solution was to use a generated column to create a UUID that’s the (agencies.objectId + the user entered reference), this way any reference user creates will automatically be unique to the table. I was trying to avoid having to add separate handler to add in references and see if I could handle this directly on the database. Suggestions welcomed.
Hello @Bob_Leung,
you cannot generate a column by referencing a column from another table. A generated column can only be generated from columns from the same table. That is probably the reason why the objectId from this table is used.
Regards, Joerg
Ok. It would be a useful feature. But for now I can add a logic in my code to manually add the reference in. Thanks.
Hi @Bob_Leung ,
Jorg Beyer is right - in generated columns you can reference only columns from table to which generated column belongs. If you want to add some data from related record you can do it in next ways:
- Via dynamic properties in each query where you need that data.
2.Using “beforeCreate/beforeUpdate” event handlers from CloudCode in case when you need to store that data in table and put some constrains on it.
Regards, Andriy
For solution (2) - I did think about doing that, however, I wasn’t sure how I can take the data passed to create the table and then add some logic to augment it.
Is there an example or documentation I can look at.
There is a documentation which covers this topic - Overview - Developing Backendless Server Code with node.js
Such event handler can be written in Js/Codeless/Java