Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logical error - why need to dublicate string? #1271

Closed
Alexufo opened this issue Aug 22, 2012 · 5 comments
Closed

Logical error - why need to dublicate string? #1271

Alexufo opened this issue Aug 22, 2012 · 5 comments

Comments

@Alexufo
Copy link

Alexufo commented Aug 22, 2012

'usersdd1' => array (self::BELONGS_TO, 'Users', 'userid'),
'clothingsizename'=>array(self::HAS_ONE, 'Clothingsize',array('clothing_size'=>'id'),'through'=>'usersdd1'),
'usersdd2' => array (self::BELONGS_TO, 'Users', 'userid'),
'shoessizesname'=>array(self::HAS_ONE, 'Shoessizes',array('shoes_size'=>'id'),'through'=>'usersdd2'),

  • usersdd1 and usersdd2 can not be join in one usersdd - "unique name error".
@cebe
Copy link
Member

cebe commented Aug 22, 2012

Didn't get what you are trying to do...
Please elaborate more what this is about.

@gregmolnar
Copy link
Contributor

@Alexufo Why would you define the exact same relation on 2 different names?

@Alexufo
Copy link
Author

Alexufo commented Aug 23, 2012

@cebe
Impossible to use one name. Yii fails. Have to make two different names

@cebe
Copy link
Member

cebe commented Aug 23, 2012

One name for what? Please be more verbose, post your code and explain in detail.

Are you trying to tell me that these relations do not work?

'usersdd' => array (self::BELONGS_TO, 'Users', 'userid'),
'clothingsizename'=>array(self::HAS_ONE, 'Clothingsize',array('clothing_size'=>'id'),'through'=>'usersdd'),
'shoessizesname'=>array(self::HAS_ONE, 'Shoessizes',array('shoes_size'=>'id'),'through'=>'usersdd'),

@resurtm
Copy link
Contributor

resurtm commented Nov 11, 2012

Additional information on this bug

Better bug description and reproduction for those going to fix this.

ER diagram:

MySQL DDL:

CREATE TABLE `tbl_kind1` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(100) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `tbl_kind2` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `kind1_id` int(11) NOT NULL,
  `title` varchar(100) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `fk-kind2-kind1` (`kind1_id`),
  CONSTRAINT `fk-kind2-kind1` FOREIGN KEY (`kind1_id`) REFERENCES `tbl_kind1` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `tbl_kind3` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `kind2_id` int(11) NOT NULL,
  `title` varchar(100) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `fk-kind3-kind2` (`kind2_id`),
  CONSTRAINT `fk-kind3-kind2` FOREIGN KEY (`kind2_id`) REFERENCES `tbl_kind2` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `tbl_kind4` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `kind2_id` int(11) NOT NULL,
  `title` varchar(100) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `fk-kind4-kind2` (`kind2_id`) USING BTREE,
  CONSTRAINT `fk-kind4-kind2` FOREIGN KEY (`kind2_id`) REFERENCES `tbl_kind2` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

MySQL DML:

SET FOREIGN_KEY_CHECKS=0;

INSERT INTO `tbl_kind1` VALUES ('1', 'kind1-1');
INSERT INTO `tbl_kind1` VALUES ('2', 'kind1-2');
INSERT INTO `tbl_kind1` VALUES ('3', 'kind1-3');

INSERT INTO `tbl_kind2` VALUES ('1', '1', 'kind2-1');
INSERT INTO `tbl_kind2` VALUES ('2', '2', 'kind2-2');
INSERT INTO `tbl_kind2` VALUES ('3', '2', 'kind2-3');
INSERT INTO `tbl_kind2` VALUES ('4', '1', 'kind2-4');

INSERT INTO `tbl_kind3` VALUES ('1', '1', 'kind3-1');
INSERT INTO `tbl_kind3` VALUES ('2', '2', 'kind3-2');
INSERT INTO `tbl_kind3` VALUES ('3', '2', 'kind3-4');
INSERT INTO `tbl_kind3` VALUES ('4', '3', 'kind3-3');
INSERT INTO `tbl_kind3` VALUES ('5', '1', 'kind3-5');
INSERT INTO `tbl_kind3` VALUES ('6', '3', 'kind3-6');
INSERT INTO `tbl_kind3` VALUES ('7', '4', 'kind3-8');
INSERT INTO `tbl_kind3` VALUES ('8', '4', 'kind3-7');

INSERT INTO `tbl_kind4` VALUES ('1', '4', 'kind4-1');
INSERT INTO `tbl_kind4` VALUES ('2', '1', 'kind4-2');
INSERT INTO `tbl_kind4` VALUES ('3', '2', 'kind4-3');
INSERT INTO `tbl_kind4` VALUES ('4', '2', 'kind4-4');
INSERT INTO `tbl_kind4` VALUES ('5', '3', 'kind4-5');
INSERT INTO `tbl_kind4` VALUES ('6', '1', 'kind4-6');
INSERT INTO `tbl_kind4` VALUES ('7', '3', 'kind4-7');
INSERT INTO `tbl_kind4` VALUES ('8', '4', 'kind4-8');

SET FOREIGN_KEY_CHECKS=1;

Gii generated relations:

// Kind1 model
return array(
    'kind2s' => array(self::HAS_MANY, 'Kind2', 'kind1_id'),
);

// Kind2 model
return array(
    'kind1' => array(self::BELONGS_TO, 'Kind1', 'kind1_id'),
    'kind3s' => array(self::HAS_MANY, 'Kind3', 'kind2_id'),
    'kind4s' => array(self::HAS_MANY, 'Kind4', 'kind2_id'),
);

// Kind3 model
return array(
    'kind2' => array(self::BELONGS_TO, 'Kind2', 'kind2_id'),
);

// Kind4 model
return array(
    'kind2' => array(self::BELONGS_TO, 'Kind2', 'kind2_id'),
);

Additional relations that could be added by developer:

// Kind1 model
return array(
    'kind2s' => array(self::HAS_MANY, 'Kind2', 'kind1_id'), // existing old relation
    'kind3s' => array(self::HAS_MANY, 'Kind3', array('id'=>'kind2_id'), 'through'=>'kind2s'),
    'kind4s' => array(self::HAS_MANY, 'Kind4', array('id'=>'kind2_id'), 'through'=>'kind2s'),
);

Lazy loading (works as expected without problems):

$kind1=Kind1::model()->findByPk(2);

echo "Kind3:\n";
foreach($kind1->kind3s as $kind3)
    echo "\t".$kind3->title."\n";

echo "Kind4:\n";
foreach($kind1->kind4s as $kind4)
    echo "\t".$kind4->title."\n";

Produces following SQL:

SELECT * FROM `tbl_kind1` `t` WHERE `t`.`id`=2 LIMIT 1;

SELECT `kind3s`.`id`       AS `t1_c0`,
       `kind3s`.`kind2_id` AS `t1_c1`,
       `kind3s`.`title`    AS `t1_c2`
FROM   `tbl_kind3` `kind3s`
       LEFT OUTER JOIN `tbl_kind2` `kind2s`
                    ON ( `kind2s`.`id` = `kind3s`.`kind2_id` )
WHERE  ( `kind2s`.`kind1_id` = :ypl0 );

SELECT `kind4s`.`id`       AS `t1_c0`,
       `kind4s`.`kind2_id` AS `t1_c1`,
       `kind4s`.`title`    AS `t1_c2`
FROM   `tbl_kind4` `kind4s`
       LEFT OUTER JOIN `tbl_kind2` `kind2s`
                    ON ( `kind2s`.`id` = `kind4s`.`kind2_id` )
WHERE  ( `kind2s`.`kind1_id` = :ypl0 );  

Eager loading (causes the problem):

$kind1=Kind1::model()
    ->with(array('kind3s','kind4s'))
    ->findByPk(2);

echo "Kind3:\n";
foreach($kind1->kind3s as $kind3)
    echo "\t".$kind3->title."\n";

echo "Kind4:\n";
foreach($kind1->kind4s as $kind4)
    echo "\t".$kind4->title."\n";

Produces following SQL:

SELECT `t`.`id`            AS `t0_c0`,
       `t`.`title`         AS `t0_c1`,
       `kind3s`.`id`       AS `t1_c0`,
       `kind3s`.`kind2_id` AS `t1_c1`,
       `kind3s`.`title`    AS `t1_c2`,
       `kind4s`.`id`       AS `t3_c0`,
       `kind4s`.`kind2_id` AS `t3_c1`,
       `kind4s`.`title`    AS `t3_c2`
FROM   `tbl_kind1` `t`
       LEFT OUTER JOIN `tbl_kind2` `kind2s`
                    ON ( `kind2s`.`kind1_id` = `t`.`id` )
       LEFT OUTER JOIN `tbl_kind3` `kind3s`
                    ON ( `kind2s`.`id` = `kind3s`.`kind2_id` )
       LEFT OUTER JOIN `tbl_kind2` `kind2s`
                    ON ( `kind2s`.`kind1_id` = `t`.`id` )
       LEFT OUTER JOIN `tbl_kind4` `kind4s`
                    ON ( `kind2s`.`id` = `kind4s`.`kind2_id` )
WHERE  ( `t`.`id` = 2 );  

Error text:

1066 Not unique table/alias: 'kind2s'

Problem is that JOIN with tbl_kind2 table made twice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants