Ticket #963 (closed defect: fixed)

Opened 15 months ago

Last modified 14 months ago

Generate Sql produces incorrect sql

Reported by: jfung Owned by: jwage
Priority: critical Milestone: 0.11.0
Component: Tasks Version: 0.11.0
Severity: Keywords:
Cc: Has Test:
Status: Has Patch:

Description

After running the command generate-sql to create an sql file, the generated sql misses certain contraints. Here's the example

User.yml

---
User:
  columns:
    id:
      type: integer(4)
      primary: true
      autoincrement: true
    username:
      type: string(255)
    password:
      type: string(255)
      
Email:
  columns:
    user_id: 
      type: integer(4)
      primary: true
    address: 
      type: string(255)
  relations:
    User:
      local: user_id
      foreign: id
      foreignType: one
      onDelete: CASCADE

BaseUser?.php

abstract class BaseUser extends Doctrine_Record
{

  public function setTableDefinition()
  {
    $this->setTableName('user');
    $this->hasColumn('id', 'integer', 4, array('primary' => true, 'autoincrement' => true));
    $this->hasColumn('username', 'string', 255);
    $this->hasColumn('password', 'string', 255);
  }

  public function setUp()
  {
    parent::setUp();
    $this->hasOne('Email', array('local' => 'id',
                                 'foreign' => 'user_id'));
  }

}

BaseEmail?.php

abstract class BaseEmail extends Doctrine_Record
{

  public function setTableDefinition()
  {
    $this->setTableName('email');
    $this->hasColumn('user_id', 'integer', 4, array('primary' => true));
    $this->hasColumn('address', 'string', 255);
  }

  public function setUp()
  {
    parent::setUp();
    $this->hasOne('User', array('local' => 'user_id',
                                'foreign' => 'id',
                                'onDelete' => 'CASCADE'));
  }

}

Generated SQL

CREATE TABLE user (id INTEGER PRIMARY KEY AUTOINCREMENT, username VARCHAR(255), password VARCHAR(255));
CREATE TABLE email (user_id INTEGER, address VARCHAR(255), PRIMARY KEY(user_id));

Change History

Changed 15 months ago by jwage

  • version changed from 0.10 to 0.11
  • milestone changed from 0.11.1 to 0.11.0

Changed 15 months ago by jwage

Hmm. This is because the user_id is set to be primary and is of the relation class Doctrine_Relation_ForeignKey and only foreign keys are created for Doctrine_Relation_LocalKey. If you remove the primary from user_id then it works as expected. I am not sure right now if we should change this or what it would affect. I'll report back here once I've had a chance to talk it over and think about it.

Changed 14 months ago by romanb

  • status changed from new to closed
  • resolution set to fixed

(In [4319]) Fixed #963. Since it is impossible for Doctrine to determine where the foreign key resides we added a new option that can be used in such scenarios to help Doctrine: owningSide => true. Please refer to the ticket testcase and the introduction of chapter 4, Relations, for the usage.

Note: See TracTickets for help on using tickets.