#1093: Possible erroneous foreign key generation
---------------------------+------------------------------------------------
  Reporter:  rafaelfuchs   |       Owner:  jwage                
      Type:  defect        |      Status:  closed               
  Priority:  minor         |   Milestone:  1.0.4                
 Component:  Schema Files  |     Version:  1.0.2                
Resolution:  invalid       |    Keywords:                       
  Has_test:  0             |    Mystatus:  Pending Core Response
 Has_patch:  0             |  
---------------------------+------------------------------------------------
Changes (by guilhermeblanco):

  * status:  new => closed
  * resolution:  => invalid

Old description:

> I have the following table defintion:
>
> {{{
> class Entidade extends Doctrine_Record {
>
>         public function setTableDefinition() {
>                 $this->hasColumn('id_entidade', 'integer', 10,
> array('primary', 'sequence' => 'SEQ_ENTIDADE'));
>                 $this->hasColumn('nome', 'string', 250);
>                 $this->hasColumn('id_grupo', 'integer', 10);
>         }
>
>         public function setUp()
>     {
>         $this->hasOne('Grupo as Grupo', array('local' => 'id_grupo',
> 'foreign' => 'id_grupo'));
>         $this->hasMany('ConfiguracaoTelefone as ConfiguracaoTelefones',
> array('local' => 'id_entidade', 'foreign' => 'id_entidade'));
>     }
>
> }
>
> class Carga extends Entidade {
>
>         public function setTableDefinition() {
>                 $this->hasColumn('descricao', 'clob', null);
>                 $this->hasColumn('peso', 'integer', 10);
>                 $this->hasColumn('material', 'string', 250);
>         }
>
> }
>
> class Pessoa extends Entidade {
>
>         public function setTableDefinition() {
>                 $this->hasColumn('cpf', 'string', 14);
>                 $this->hasColumn('rg', 'integer', 10);
>         }
>
> }
>
> }}}
>
> Note that the ENTIDADE entity has a foregin key ID_GRUPO.
> The entities PESSOA and CARGA extends ENTIDADE.
>
> The SQL Script generated by Doctrine is as below.
> I'm omitting the irrelevant lines.
>
> CREATE TABLE pessoa (id_entidade NUMBER(10), cpf VARCHAR2(14), rg
> NUMBER(10), PRIMARY KEY(id_entidade));
>
> CREATE TABLE entidade (id_entidade NUMBER(10), nome VARCHAR2(250),
> id_grupo NUMBER(10), PRIMARY KEY(id_entidade));
>
> CREATE TABLE carga (id_entidade NUMBER(10), descricao CLOB, peso
> NUMBER(10), material VARCHAR2(250), PRIMARY KEY(id_entidade));
>
> '''ALTER TABLE pessoa ADD FOREIGN KEY (id_grupo) REFERENCES
> grupos(id_grupo) NOT DEFERRABLE INITIALLY IMMEDIATE;'''
>
> ALTER TABLE entidade ADD FOREIGN KEY (id_grupo) REFERENCES
> grupos(id_grupo) NOT DEFERRABLE INITIALLY IMMEDIATE;
>
> '''ALTER TABLE carga ADD FOREIGN KEY (id_grupo) REFERENCES
> grupos(id_grupo) NOT DEFERRABLE INITIALLY IMMEDIATE;'''
>
> Note the bold lines.
>
> Am I doing something wrong or expecting a different behavior from
> Doctrine?
>
> In my point of view the bold lines shouldn't be generated. The foreign
> key belongs only to ENTIDADE.

New description:

 I have the following table defintion:

 {{{
 class Entidade extends Doctrine_Record {

         public function setTableDefinition() {
                 $this->hasColumn('id_entidade', 'integer', 10,
 array('primary', 'sequence' => 'SEQ_ENTIDADE'));
                 $this->hasColumn('nome', 'string', 250);
                 $this->hasColumn('id_grupo', 'integer', 10);
         }

         public function setUp()
     {
         $this->hasOne('Grupo as Grupo', array('local' => 'id_grupo',
 'foreign' => 'id_grupo'));
         $this->hasMany('ConfiguracaoTelefone as ConfiguracaoTelefones',
 array('local' => 'id_entidade', 'foreign' => 'id_entidade'));
     }

 }

 class Carga extends Entidade {

         public function setTableDefinition() {
                 $this->hasColumn('descricao', 'clob', null);
                 $this->hasColumn('peso', 'integer', 10);
                 $this->hasColumn('material', 'string', 250);
         }

 }

 class Pessoa extends Entidade {

         public function setTableDefinition() {
                 $this->hasColumn('cpf', 'string', 14);
                 $this->hasColumn('rg', 'integer', 10);
         }

 }

 }}}

 Note that the ENTIDADE entity has a foregin key ID_GRUPO.
 The entities PESSOA and CARGA extends ENTIDADE.

 The SQL Script generated by Doctrine is as below.
 I'm omitting the irrelevant lines.

 CREATE TABLE pessoa (id_entidade NUMBER(10), cpf VARCHAR2(14), rg
 NUMBER(10), PRIMARY KEY(id_entidade));

 CREATE TABLE entidade (id_entidade NUMBER(10), nome VARCHAR2(250),
 id_grupo NUMBER(10), PRIMARY KEY(id_entidade));

 CREATE TABLE carga (id_entidade NUMBER(10), descricao CLOB, peso
 NUMBER(10), material VARCHAR2(250), PRIMARY KEY(id_entidade));

 '''ALTER TABLE pessoa ADD FOREIGN KEY (id_grupo) REFERENCES
 grupos(id_grupo) NOT DEFERRABLE INITIALLY IMMEDIATE;'''

 ALTER TABLE entidade ADD FOREIGN KEY (id_grupo) REFERENCES
 grupos(id_grupo) NOT DEFERRABLE INITIALLY IMMEDIATE;

 '''ALTER TABLE carga ADD FOREIGN KEY (id_grupo) REFERENCES
 grupos(id_grupo) NOT DEFERRABLE INITIALLY IMMEDIATE;'''

 Note the bold lines.

 Am I doing something wrong or expecting a different behavior from
 Doctrine?

 In my point of view the bold lines shouldn't be generated. The foreign key
 belongs only to ENTIDADE.

Comment:

 This is the expected behavior, since you're trying to do what we call as
 Class Table Inheritance, which is not correctly implemented, so... not
 supported!

 The issue specifically is related to your inheritance.

 Carga extends Entidade (and setUp not overriden)
 Pessoas extends Entidade (and setUp not overriden)

 So it'll try to create the constraints and will fail.
 The solution to is try another type of inheritance, like Column
 Aggregation.


 Regards,

-- 
Ticket URL: <http://trac.doctrine-project.org/ticket/1093#comment:10>
Doctrine <http://www.phpdoctrine.org>
PHP Doctrine Object Relational Mapper
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"doctrine-svn" group.
 To post to this group, send email to [email protected]
 To unsubscribe from this group, send email to [EMAIL PROTECTED]
 For more options, visit this group at 
http://groups.google.co.uk/group/doctrine-svn?hl=en-GB
-~----------~----~----~----~------~----~------~--~---

Reply via email to