You are viewing a plain text version of this content. The canonical link for it is here.
Posted to torque-user@db.apache.org by levko <le...@yahoo.com> on 2009/09/03 22:43:33 UTC

Primary key is set to the foreign key value

I have very simple DB schema, where the 1st table (Person) has PK 'id' and
the 2nd table (Person_SSN) has FK 'person_id' referring to the 'id' field of
the 'Person' table. The 'Person_SSN' table has its own PK 'ssn':

<database name="test" defaultIdMethod="idbroker"> 
  <table name="Person" description="Person Data"> 
    <column name="ID" 
      javaName="PersonId"
      required="true" 
      primaryKey="true" 
      type="INTEGER" 
      description="Person ID"/> 
    <column name="FULL_NAME" 
      javaName="FullName"
      required="true" 
      type="VARCHAR" size="128" 
      description="Full Name"/> 
    <column name="EMAIL" 
      javaName="EMailAddress"
      required="true" 
      type="VARCHAR" size="128" 
      description="EMail Address"/> 
    <unique> 
      <unique-column name="EMAIL"/> 
    </unique> 
  </table> 
  <table name="Person_SSN" description="Person SSN">
    <column name="PERSON_ID"
      javaName="PersonId"
      required="true"
      type="INTEGER"
      description="Foreign key Person"/>
    <column name="SSN"
      required="true"
      primaryKey="true" 
      type="CHAR" size="9"
      description="SSN"/>
    <foreign-key foreignTable="Person" onDelete="cascade">
      <reference local="PERSON_ID" foreign="ID"/> 
    </foreign-key>
  </table>
</database>

When I create 'PersonSsn' object, I set SSN to '123456789', but the DB
record shows that  SSN value is the same as the PERSON_ID value. I use
Torque 3.3 and PostgreSQL 8.4.0.

Could you, please, help me to understand what's the problem here?
I've uploaded the source files. The test driver is in the lk.test.TestOM
class.

Thanks in advance for any idea,
-- Lev http://www.nabble.com/file/p25283761/Test.zip Test.zip 
-- 
View this message in context: http://www.nabble.com/Primary-key-is-set-to-the-foreign-key-value-tp25283761p25283761.html
Sent from the Apache DB - Torque Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: torque-user-unsubscribe@db.apache.org
For additional commands, e-mail: torque-user-help@db.apache.org


Re: Primary key is set to the foreign key value

Posted by levko <le...@yahoo.com>.
Thanks everybody for useful suggestions. 

Actually, the tables I use have more complicated structures (each table has
it's own id key, etc.). I just wanted to understand why the char field.
which I defined as PK, is automatically filled with int value by the
idBroker. I believe, it's not intuitive behavior - I expected that idBroker
would, at least, check that the object I create and save already has PK
field set and would not generate new id automatically. 

In any case, the idMethod="none" attribute makes it more flexible.

Thanks again,
-- Lev
-- 
View this message in context: http://www.nabble.com/Primary-key-is-set-to-the-foreign-key-value-tp25283761p25317361.html
Sent from the Apache DB - Torque Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: torque-user-unsubscribe@db.apache.org
For additional commands, e-mail: torque-user-help@db.apache.org


Re: Primary key is set to the foreign key value

Posted by Thomas Vandahl <tv...@apache.org>.
Adam Allgaier wrote:
> Change the table definition to this:
> 
>   <table name="Person_SSN" description="Person SSN" idMethod="none">
> 
> You are generating the PK value yourself, so you don't need Torque or Postgres to do it for you.

Right. The Torque idbroker is made for numerical primary keys. Your
primary key is CHAR. Are you sure you want to do this?

>   <table name="Person_SSN" description="Person SSN">
[...]
>     <column name="SSN"
>       required="true"
>       primaryKey="true" 
>       type="CHAR" size="9"
              ^^^^
>       description="SSN"/>

Bye, Thomas.

---------------------------------------------------------------------
To unsubscribe, e-mail: torque-user-unsubscribe@db.apache.org
For additional commands, e-mail: torque-user-help@db.apache.org


Re: Primary key is set to the foreign key value

Posted by Adam Allgaier <al...@yahoo.com>.
Change the table definition to this:

  <table name="Person_SSN" description="Person SSN" idMethod="none">

You are generating the PK value yourself, so you don't need Torque or Postgres to do it for you.

Adam





----- Original Message ----
From: levko <le...@yahoo.com>
To: torque-user@db.apache.org
Sent: Thursday, September 3, 2009 3:43:33 PM
Subject: Primary key is set to the foreign key value


I have very simple DB schema, where the 1st table (Person) has PK 'id' and
the 2nd table (Person_SSN) has FK 'person_id' referring to the 'id' field of
the 'Person' table. The 'Person_SSN' table has its own PK 'ssn':

<database name="test" defaultIdMethod="idbroker"> 
  <table name="Person" description="Person Data"> 
    <column name="ID" 
      javaName="PersonId"
      required="true" 
      primaryKey="true" 
      type="INTEGER" 
      description="Person ID"/> 
    <column name="FULL_NAME" 
      javaName="FullName"
      required="true" 
      type="VARCHAR" size="128" 
      description="Full Name"/> 
    <column name="EMAIL" 
      javaName="EMailAddress"
      required="true" 
      type="VARCHAR" size="128" 
      description="EMail Address"/> 
    <unique> 
      <unique-column name="EMAIL"/> 
    </unique> 
  </table> 
  <table name="Person_SSN" description="Person SSN">
    <column name="PERSON_ID"
      javaName="PersonId"
      required="true"
      type="INTEGER"
      description="Foreign key Person"/>
    <column name="SSN"
      required="true"
      primaryKey="true" 
      type="CHAR" size="9"
      description="SSN"/>
    <foreign-key foreignTable="Person" onDelete="cascade">
      <reference local="PERSON_ID" foreign="ID"/> 
    </foreign-key>
  </table>
</database>

When I create 'PersonSsn' object, I set SSN to '123456789', but the DB
record shows that  SSN value is the same as the PERSON_ID value. I use
Torque 3.3 and PostgreSQL 8.4.0.

Could you, please, help me to understand what's the problem here?
I've uploaded the source files. The test driver is in the lk.test.TestOM
class.

Thanks in advance for any idea,
-- Lev http://www.nabble.com/file/p25283761/Test.zip Test.zip 
-- 
View this message in context: http://www.nabble.com/Primary-key-is-set-to-the-foreign-key-value-tp25283761p25283761.html
Sent from the Apache DB - Torque Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: torque-user-unsubscribe@db.apache.org
For additional commands, e-mail: torque-user-help@db.apache.org


      

---------------------------------------------------------------------
To unsubscribe, e-mail: torque-user-unsubscribe@db.apache.org
For additional commands, e-mail: torque-user-help@db.apache.org


RE: Primary key is set to the foreign key value

Posted by Thomas Fischer <fi...@seitenbau.net>.
> I have very simple DB schema, where the 1st table (Person) has PK 'id'
and
> the 2nd table (Person_SSN) has FK 'person_id' referring to the 'id' field
of
> the 'Person' table.
>
> ......
>
> When I create 'PersonSsn' object, I set SSN to '123456789', but the DB
> record shows that  SSN value is the same as the PERSON_ID value. I use
> Torque 3.3 and PostgreSQL 8.4.0.

I have never heard of such behaviour before nor have I experienced it
myself.

Do you have the opportunity to debug the application ? If yes, you can set
a debug breakpoint in the setter of the PERSON_ID value in BasePersonSsn to
find out where the value comes from.
If this method is not called, set a breakpoint in the save method and see
whether the PERSON_ID is already set at the start of the method and whether
it is set in the method.

   Hope that helps,

           Thomas


---------------------------------------------------------------------
To unsubscribe, e-mail: torque-user-unsubscribe@db.apache.org
For additional commands, e-mail: torque-user-help@db.apache.org


RE: Primary key is set to the foreign key value

Posted by Greg Monroe <Gr...@dukece.com>.
Hmm, only thing that I can think of is that you are using the
id broker method to autoincrement the numeric ids.  This may be
replacing the SSN with a number when you save the object.  Try 
adding the following attribute to your SSN column:

autoIncrement="false"

You might also consider using the "native" mode for autoincrement
as most modern SQL servers support this and it's faster and more
reliable than the idBroker code.

Also, from a general design perspective, you might want to add a 
separate ID column the Person_SSN table rather than use the SSN as 
the primary key. You can get into legal trouble is you pass SSN's 
around as keys and they become visible.  Much safer to have a 
unique ID not related to the SSN that can be passed.

> -----Original Message-----
> From: levko [mailto:lev.kozakov@yahoo.com]
> Sent: Thursday, September 03, 2009 4:44 PM
> To: torque-user@db.apache.org
> Subject: Primary key is set to the foreign key value
> 
> 
> I have very simple DB schema, where the 1st table (Person) has PK 'id'
> and
> the 2nd table (Person_SSN) has FK 'person_id' referring to the 'id' field
> of
> the 'Person' table. The 'Person_SSN' table has its own PK 'ssn':
> 
> <database name="test" defaultIdMethod="idbroker">
>   <table name="Person" description="Person Data">
>     <column name="ID"
>       javaName="PersonId"
>       required="true"
>       primaryKey="true"
>       type="INTEGER"
>       description="Person ID"/>
>     <column name="FULL_NAME"
>       javaName="FullName"
>       required="true"
>       type="VARCHAR" size="128"
>       description="Full Name"/>
>     <column name="EMAIL"
>       javaName="EMailAddress"
>       required="true"
>       type="VARCHAR" size="128"
>       description="EMail Address"/>
>     <unique>
>       <unique-column name="EMAIL"/>
>     </unique>
>   </table>
>   <table name="Person_SSN" description="Person SSN">
>     <column name="PERSON_ID"
>       javaName="PersonId"
>       required="true"
>       type="INTEGER"
>       description="Foreign key Person"/>
>     <column name="SSN"
>       required="true"
>       primaryKey="true"
>       type="CHAR" size="9"
>       description="SSN"/>
>     <foreign-key foreignTable="Person" onDelete="cascade">
>       <reference local="PERSON_ID" foreign="ID"/>
>     </foreign-key>
>   </table>
> </database>
> 
> When I create 'PersonSsn' object, I set SSN to '123456789', but the DB
> record shows that  SSN value is the same as the PERSON_ID value. I use
> Torque 3.3 and PostgreSQL 8.4.0.
> 
> Could you, please, help me to understand what's the problem here?
> I've uploaded the source files. The test driver is in the lk.test.TestOM
> class.
> 
> Thanks in advance for any idea,
> -- Lev http://www.nabble.com/file/p25283761/Test.zip Test.zip
> --
> View this message in context: http://www.nabble.com/Primary-key-is-set-
> to-the-foreign-key-value-tp25283761p25283761.html
> Sent from the Apache DB - Torque Users mailing list archive at
> Nabble.com.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: torque-user-unsubscribe@db.apache.org
> For additional commands, e-mail: torque-user-help@db.apache.org

DukeCE Privacy Statement:
Please be advised that this e-mail and any files transmitted with
it are confidential communication or may otherwise be privileged or
confidential and are intended solely for the individual or entity
to whom they are addressed. If you are not the intended recipient
you may not rely on the contents of this email or any attachments,
and we ask that you please not read, copy or retransmit this
communication, but reply to the sender and destroy the email, its
contents, and all copies thereof immediately. Any unauthorized
dissemination, distribution or copying of this communication is
strictly prohibited.

---------------------------------------------------------------------
To unsubscribe, e-mail: torque-user-unsubscribe@db.apache.org
For additional commands, e-mail: torque-user-help@db.apache.org