ForestAdmin / forest-express-sequelize

🌱 Express/Sequelize agent for Forest Admin to integrate directly to your existing Express/Sequelize backend application.
https://www.forestadmin.com
GNU General Public License v3.0
190 stars 47 forks source link

[*] Sequelize Getters - Support getters that return Promises for belongsTo relationships #206

Open nick-keller opened 6 years ago

nick-keller commented 6 years ago

Expected behavior

I have a table (let's say Post) with a foreign key to another table (let's say Author). The "REFERENCE FIELD" of the table Author is set to the username. I expect to see the username in the column author of each post and on the "details" view of each post.

 Actual behavior

Even tho the foreign key in the database is present, the author is missing from both views (table, and details)

Context

arnaudbesnier commented 6 years ago

Hi @nick-keller, this is a feature used a lot and nobody else reported such issue recently. It might be a configuration issue.

Can you send us the name of your Forest project to dig into this?

Thanks!

nick-keller commented 6 years ago

Hi @arnaudbesnier, the name of the project is Equify, one of the table where is fails is the Role table with the column User.

Thanks for your help ;)

nick-keller commented 6 years ago

Ok so I figured out what the problem was. Using sequelize I wrote a getter for the relation Author that returns a promise.

The getter is very simple and allows you to write await post.author at anytime to get the author.

In both cases a promise is returned (Not needed in case 1 but it makes the code omogenus).

Solution

On my side I can make sure it does not return a promise if the data is already fetched. Simple but against my code guidelines.

On Forest side an await or a then can be added.

arnaudbesnier commented 6 years ago

Thanks @nick-keller for the details, we've never tested the Sequelize getter feature compatibility with Forest. Sorry you did this investigation work for us.

Let's keep this issue open until a fix is implemented.

nick-keller commented 6 years ago

No problem, I understand this can be an edge case. I'll follow the issue ;)