Nut icon indicating copy to clipboard operation
Nut copied to clipboard

Foreign key objects in Nut::Row

Open yusufmk opened this issue 3 years ago • 2 comments

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<Comment> 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<Comment> c = db.comments()->query()->join<Post>()->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.

yusufmk avatar Oct 24 '20 12:10 yusufmk

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

HamedMasafi avatar Oct 24 '20 12:10 HamedMasafi

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.

yusufmk avatar Oct 24 '20 12:10 yusufmk