Related data not showing up

Hi there,
We are having some issue with related data :

Context

We have a one-to-many relationship between an Artist and his Medias generated by Lumber (I don’t see any problem in the generated models)
The relationship itself works perfectly fine, I can locally access the medias when calling [forestUrl]artists/[artistId]/relationships/artistsMedias/

Moreover, the ArtistMedia table is correctly linked to the corresponding artist (that being said, we also have an issue here, because the artist name does not display right away, we have to go to the media details and then back for it to be displayed properly, I’ll dig into this later)

When i’m opening an artist detail, I can see the related table under the related data section, but when going there no record is shown.
It doesn’t look like they are not found, because the searchbar shows the right number of medias ("search within XXX medias), rather it looks like no call is made to retrieve the medias.

When looking at the network tab, I can see :
1- The call to /artist/[artistId] that gets the attributes and relationships (amongst which the Medias)
2- When going to the related data tab, the call to /artists/[artistId]/relationships/media/count that gets the right number (thus the right number in the searchbar, i suppose)
But no call to get the actual medias

My guess is that no call is made if the related data is not set to be seen in the UI settings, but I don’t get why nothing is displayed in this case, since everything seems to work fine.

Should they be retrieved in one go when getting the artist details, or is there a trick to perform an additional call to get the medias?

If i am being unclear, I will provide some screenshots for explanation :slight_smile:

Thanks in advance!

  • Package Version:
  • Express Version: 4.16
  • Sequelize Version: 5.15
  • Database Dialect:
  • Database Version:
  • Project Name:

Hi @jcastagne ! Can you share some screenshots or a video so that I can better understand your issue ?

Sure!

Here is the Media table (although not displaying, all three belongs to the same artist, as I mentioned in the initial post, I don’t think that this is related, and probably has something to do with the media resolver not loading the associated artist before rendering I guess)

Here is my artist details, where I can see the Media as related data

And finally this is what is displayed when I navigate to the Artist Medias from the artist details. I would expect the three medias to show here, but nothing is displayed.

Ok thanks, can you share with me your models definition ?
Did you make any recent change ?

//models/artist

 const Artists = sequelize.define('artists', {
    id: {
      type: DataTypes.UUID,
      primaryKey: true,
      defaultValue: Sequelize.literal('uuid_generate_v4()'),
      allowNull: false,
    },
// Bunch of other fields
{
    tableName: 'artists',
    underscored: true,
    schema: process.env.DATABASE_SCHEMA,
  });

Artists.associate = (models) => {
    Artists.hasMany(models.artistsMedias, {
      foreignKey: {
        name: 'artistIdKey',
        field: 'artist_id',
      },
      as: 'artistsMedias',
    });

As for the Media :

const ArtistsMedias = sequelize.define('artistsMedias', {
    id: {
      type: DataTypes.UUID,
      primaryKey: true,
      defaultValue: Sequelize.literal('uuid_generate_v4()'),
      allowNull: false,
    },
// Bunch of other fields
 {
    tableName: 'artists_medias',
    underscored: true,
    schema: process.env.DATABASE_SCHEMA,
  });

ArtistsMedias.associate = (models) => {
    ArtistsMedias.belongsTo(models.artists, {
      foreignKey: {
        name: 'artistIdKey',
        field: 'artist_id',
      },
      as: 'artist',
    });
  };

We recently added some smart actions to the artists, but no recent changes to the models itself

What bugs me is that it seems perfectly able to interpret the relationship (and to fetch the right number of Medias), and as far as I tested locally, calling

[forestUrl]/artists/[artistId]/relationships/artistsMedias/

does return the three media related to this artist

Your artistMedia table looks like a many-to-many table, is it one ?

No, that’s a Artist (one) - (many) artistMedia relationship

And you have no error in your logs ?

Nope, neither http errors showing in the network tab, nor error logged by our back container. I see the same thing in the logs as in the network though, which is that the actual media data is never queried (I can see the logs for the artist request and the media count, but nothing querying the media itself)

Did you hide one of the concerned collection in your layout ?

Both of them are visible in the main layout, if that is your question
Main dashboard visibility

I see that you are using segments, is your collection restricted to segment only ?
image

No, here are the settings for the artists

As for the artistMedia :

Hello @jcastagne ! I tried to reproduce your problem but couldn’t manage to get the behavior that you’re experiencing. I think we might need a little bit more information to investigate the problem.

Could you send me some screenshots ?

  • The browser console logs after clicking on the artist medias table in the data section
  • The browser console logs after clicking on the “related data” item of your artist

Oww my bad, there’s an error that I didn’t spot until now :

I have seen a post related to this here, i’ll try to do the same thing and see how it goes

So, as suggested in This post I made the following changes :

//models/artist-medias
const ArtistsMedias = sequelize.define('artistsMediasUrl', {...}

and

//models/artists
Artists.associate = (models) => {
    Artists.hasMany(models.artistsMediasUrl, {
      foreignKey: {
        name: 'artistIdKey',
        field: 'artist_id',
      },
      as: 'artistsMedias',
    });

And it’s now working!

The issue seemed to be that “media” was pluralized into “medium” and the resolver did not find any matching model…

Edit :
After watching the different requests made :

  • When querying the related data, we call https://[ForestUrl]/artists/[ArtistId]/relationships/artistsMedias (using the alias we gave in the artist model, seems logic)
  • When querying all the media table, we call https:/[forestUrl]/forest/artistsMediasUrl/ (seems legit as well, since I changed the model name for sequelize)

The question I am asking myself now is how it is able to get the artistMedias through this route, since I made no changes to the routes folder? :thinking:
My routes/artist-media still looks like

const express = require('express');
const { PermissionMiddlewareCreator } = require('forest-express-sequelize');
const { artistsMedias } = require('../models');
const router = express.Router();
const permissionMiddlewareCreator = new PermissionMiddlewareCreator('artistsMedias');

// Create a Artists Media
router.post('/artistsMedias', permissionMiddlewareCreator.create(), (request, response, next) => {
  next();
});

// And so on...

Since this is working and all i am just wondering in case We’ll have to add some modifications to the route resolvers later, I don’t quite understand :thinking:
Should I get rid of this file and create an artist-medias-url file in the route folder?

1 Like