OneToOne: need clarifications

Forum for posting problems using QxOrm library

OneToOne: need clarifications

Postby gianks » Fri Aug 26, 2016 4:01 pm

Hi,
i'm trying to accomplish the following:

TABLE A, id STRING, ...
TABLE B, id STRING references A.id, ...
TABLE C, id STRING references A.id, ...
...

If i have undestood correctly i have to use a OneToOne relation on A but i'm not able to make B having the counterpart shared_pointer to A since i cannot apparently create a ManyToOne relationship pointing to A sharing the DB field B.id since i'm obtaining a false assertion from the library as soon as i attempt to use the same sKey for id declaration and the relationship (which is accepted if declared before the id, which then throws the same error): ASSERT: "false" in file /usr/local/inl/QxDataMember/QxDataMemberX.inl, line 167

The documentation is really brief on this relationship.
How can i have B.id as a field (QString id) and a QSharedPointer<A> using the same DB column?

Using the partial example of the documentation, how are Author and Person supposed to be written in order to have the Author to appear like a B object (dependant ID) in respect of Person?
How can an Author access the Person obj and how can the Person object access its optional Author object?

EDIT: working with ManyToMany relationships i've been able (i'd like to know if it's supposed to work this way) to create the relevant ManyToOne/OneToMany between the ManyMany extra table and the individual linked entities and i wish you to confirm me this is the way to do this.
This seems possible since the composite primary key allows me to use the field name of each individual column since the PK is reporting its format like col1|col2|col3...
Indeed in that case the introspection engine reports to me each individual column part of the PK as NON PK while the PK is listed as a separate field.
But when i try to accomplish what i asked in the top this goes against the rule which apparently states that the ID cannot be used in a relationship too... I need some help here... :shock:


Thanks a lot
gianks
 
Posts: 45
Joined: Mon Jul 04, 2016 1:04 pm

Re: OneToOne: need clarifications

Postby qxorm » Mon Aug 29, 2016 12:02 pm

Hello,

How can i have B.id as a field (QString id) and a QSharedPointer<A> using the same DB column?

I think this is the same question as this topic in StackOverflow : http://stackoverflow.com/questions/2158 ... m-is-a-man

So you can try this code :
Code: Select all
namespace qx
{
template <> void register_class(QxClass<secondtable> & t)
{
   t.id(&secondtable::m_id, “second_table _key|first_table_key”);
   t.data(&secondtable::m_text, "second_table_text");
   qx::IxSqlRelation * pRelation = t.relationManyToOne(&secondtable::m_firsttable, “first_table”);
   pRelation->getDataMember()->setName("first_table_key");
}
}

As you can see, you can define your relation with another key (first_table), which is not the same key as your primary key (first_table_key). And then, just put the correct name of your relation using pRelation->getDataMember()->setName() method.
qxorm
Site Admin
 
Posts: 481
Joined: Mon Apr 12, 2010 7:45 am

Re: OneToOne: need clarifications

Postby gianks » Mon Aug 29, 2016 11:13 pm

Hi and thanks.
I finally ended with a double oneToOne which is working pretty well.
My need of the ManyToOne was in the attempt to create the relevant FK within my initilization functions, already working that way.

Using OneToOne both sides don't explicitly state which table leads the FK(s) but using a PropertyBag i could workaround this.

Thanks for the suggestion, if i will make new changes i'll evalutate this solution.
Kind Regards
gianks
 
Posts: 45
Joined: Mon Jul 04, 2016 1:04 pm


Return to QxOrm - Help

Who is online

Users browsing this forum: No registered users and 2 guests

cron