HamedMasafi / Nut

Advanced, Powerful and easy to use ORM for Qt
GNU Lesser General Public License v3.0
294 stars 75 forks source link

Foreign key objects in Nut::Row #116

Open yusufmk opened 3 years ago

yusufmk commented 3 years ago

Hi, I see that Nut::Row sharedpointers have fields related to their foreign keys. But they are always null. I want to be able to access those objects directly from the child table. Is this possible? Am I doing something wrong?

For example I have two tables, posts and comments: "Comment" class has "Post" as foreign key. And "Post" class has "comments" as child table. Now, when I get a comment row with; Nut::Row c = db.comments()->query()->first();

Here pointer "c" contains fields as postId and post. postId shows the related post's id. It is fine. BUT field "post" is always null. However it would be awesome if I could access to object post like this: QString pn = c->post->postname(); // currently this line throws error, the app crashes, because c->post is nullptr.

I tried "join" but result is the same. post is always null. Nut::Row c = db.comments()->query()->join()->first(); QString pn = c->post->postname(); // result is same as above.

Development platform: OS: ubuntu 18.04 Qt creator 4.13.0, based on Qt 5.15.0 Compiler GCC 5.3.1 Nut commit hash: Commit 6ce50e2 by Jack Lilhammers, 07/07/2020 05:50 PM // this is the commit you used in OrmTest repo. Serializer commit hash: Commit b0194da3 by Miklós Márton, 06/13/2020 12:00 AM // this is the commit you used in OrmTest repo.

Thank you in advance.

HamedMasafi commented 3 years ago

To join another table use template syntax; take a look at this text for example https://github.com/HamedMasafi/Nut/blob/c60d31465c97fac58305756f6d9d07f4a4bd7f2e/test/tst_basic/tst_basic.cpp#L149

yusufmk commented 3 years ago

No no no, I already know this AND this is not what I want. This is showing accessing to "comment" FROM "post" object. I want accessing to "post" FROM "comment" object.