Question on implementing a smart relationship using AfterFind hook

Hi team,

I am currently rewriting the way in which I am populating the smart fields using the AfterFind hook technique as shown at:

It seems to be a very good way to improve the performances as it will generate far fewer queries to the database.

I made it work for the vast majority of my smart fields.
However, I have a problem with smart relationships.

Let’s take my example in a few words. I have the following native relationships:


In the kyc collection, I have defined the smart relationship to get a hasOne or BelongsTo between db.kyc and

    { field: 'card', // belongsTo association from kyc to cards
      type: 'String',
      reference: 'cards.user_id', // initialized in model afterFind hook
      relationship: 'BelongsTo',

Before my changes, I had implemented a get() method that was doing the job.

      get: (kyc) => {
          .findOne({ where: { user_id: kyc.user_id } })
          .then((card) => { return card; });

Using that old method, the smart field displayed as a Reference in the Cards collection was correctly populated when displaying the Kyc rows.

Now, when using the AfterFind hook, I don’t know what I have to return in the kyc.card member.
I tried to set it like this:

  kyc.card: { id: 'd6a07fac-eaa9-4f37-9c9a-42cd991ce26f' },

When doing that, the card id is displayed in the Kyc rows, instead of the smart field defined as a Reference in the Cards collection.

So, it seems that:

  • either something is missing in the returned value
  • or the smart field of the Cards collection (used as a Reference) is not populated

Could you please help me?

Thanks and regards

Here is my setup

    "database_type": "postgres",
    "liana": "forest-express-sequelize",
    "liana_version": "6.5.0",
    "engine": "nodejs",
    "engine_version": "12.13.1",
    "orm_version": "5.22.3"
1 Like

Hi @Louis-Marie,

As far as I know, the technique you mention at the beginning of your message is for the smart fields, so you have to populate the original record (kyc in your case) with some raw value (but you are trying to apply an entire object).

I think what you are trying to do is not supported.
I am going to ask to other people more skilled in this kind of task.

Hi @Louis-Marie,

Did you try to populate the card reference field value in kyc.card to have something like:
kyc.card: { id: 'd6a07fac-eaa9-4f37-9c9a-42cd991ce26f', myReferenceField: 'displayedValue' },

Hi @arnaud ,

No I have not tried yet.
The reference field of cards collection being a smart field itself, its get() method was called with the previous implementation, but it’s no more the case, while that one is still implemented with the get() method.

If I do it, I will lose some benefit in having to get other smart field references than the current collection.

But I will try now to let you know.

Hi again @arnaud,

I confirm that it works if I add the the smart field reference in the returned object, as you suggested:

kyc.card: { id: 'd6a07fac-eaa9-4f37-9c9a-42cd991ce26f', myReferenceField: 'displayedValue' }

But to give you complete information:

  • When displaying the users collection (which has a native hasOne toward cards), I had not this problem, while I had also implemented the AfterFind hook
  • I have this problem only for the smart relationship from kyc towards cards.

So I would like to avoid the need to define the dependent smart relationships, (if it is possible :wink:).

Thanks for your help.

Hello @Louis-Marie,

In your users collections, are you adding a reference to a card inside the hook, just as you did for the kyc collection? Or did you use the hook for another purpose?

When requesting fields from a collection that has native relationships, the generated query on the database includes all the needed fields for the references to be correctly displayed. To the best of my knowledge, records are not “completed” afterwards.

Hi @GuillaumeGautreau

Sorry for my late answer.

I have been waiting for the bug fixes on the smart fields before going further on that ticket.
My new current setup is the following one:

    "database_type": "postgres",
    "liana": "forest-express-sequelize",
    "liana_version": "6.6.3",
    "engine": "nodejs",
    "engine_version": "12.13.1",
    "orm_version": "5.22.3"

I think that this issue has been resolved with the other bug fixes in 6.6.3.
Now, providing the native fields of the associated relationships required to initialize the references of the associated relationships is working fine.

Thank you for your help.

1 Like