-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Join column index names #1138
Join column index names #1138
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
<?php | ||
|
||
namespace Doctrine\Tests\ORM\Functional\Ticket; | ||
|
||
use Doctrine\ORM\Query\ResultSetMapping; | ||
|
||
class JoinColumnIndexNameTest extends \Doctrine\Tests\OrmFunctionalTestCase | ||
{ | ||
public function testCreateSchemaJoinColumnExplicitIndexName() | ||
{ | ||
$schemaTool = new \Doctrine\ORM\Tools\SchemaTool($this->_em); | ||
$sql = $schemaTool->getCreateSchemaSql(array( | ||
$this->_em->getClassMetadata(__NAMESPACE__ . '\Car'), | ||
$this->_em->getClassMetadata(__NAMESPACE__ . '\Maker'), | ||
)); | ||
|
||
$expected = array ( | ||
0 => 'CREATE TABLE Car (id INTEGER NOT NULL, maker_id INTEGER DEFAULT NULL, name VARCHAR(255) NOT NULL, year INTEGER NOT NULL, PRIMARY KEY(id), CONSTRAINT FK_4F70A07D68DA5EC3 FOREIGN KEY (maker_id) REFERENCES Maker (id) NOT DEFERRABLE INITIALLY IMMEDIATE)', | ||
1 => 'CREATE INDEX idx_maker_id ON Car (maker_id)', | ||
2 => 'CREATE TABLE Maker (id INTEGER NOT NULL, name VARCHAR(255) NOT NULL, PRIMARY KEY(id))', | ||
); | ||
|
||
$this->assertEquals( | ||
$expected, | ||
$sql | ||
); | ||
} | ||
|
||
public function testUpdateSchemaJoinColumnExplicitIndexName() | ||
{ | ||
$classes = array( | ||
$this->_em->getClassMetadata(__NAMESPACE__ . '\Car'), | ||
$this->_em->getClassMetadata(__NAMESPACE__ . '\Maker'), | ||
); | ||
|
||
$schemaTool = new \Doctrine\ORM\Tools\SchemaTool($this->_em); | ||
$schemaTool->createSchema($classes); | ||
|
||
$sql = $schemaTool->getUpdateSchemaSql($classes, true); | ||
|
||
$schema = $schemaTool->getSchemaFromMetadata($classes); | ||
|
||
$carTable = $schema->getTable('Car'); | ||
|
||
$indexes = $this->_em->getConnection()->getSchemaManager()->listTableIndexes('Car'); | ||
|
||
unset($indexes['primary']); | ||
|
||
/** @var \Doctrine\DBAL\Schema\Index $makerIdIndex */ | ||
$makerIdIndex = current($indexes); | ||
|
||
$this->_em->getConnection()->getSchemaManager()->dropIndex($makerIdIndex, $carTable); | ||
|
||
$query = $this->_em->createNativeQuery( | ||
'CREATE INDEX idx_maker_id ON Car(maker_id)', | ||
$rsm = new ResultSetMapping() | ||
); | ||
$query->execute(); | ||
|
||
$sql = $schemaTool->getUpdateSchemaSql($classes, true); | ||
|
||
$this->assertEmpty($sql, "There should not be any changes, all indexes are present."); | ||
} | ||
} | ||
|
||
/** | ||
* @Table( | ||
* indexes={@Index(name="idx_maker_id", columns={"maker_id"})}) | ||
* ) | ||
* @Entity() | ||
*/ | ||
class Car | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should prefix these classes with the test id |
||
{ | ||
/** | ||
* @Id | ||
* @GeneratedValue(strategy="IDENTITY") | ||
* @Column(type="integer", nullable=false) | ||
*/ | ||
protected $id; | ||
|
||
/** | ||
* @Column(type="string") | ||
*/ | ||
protected $name; | ||
|
||
/** | ||
* @Column(type="integer", length=4) | ||
*/ | ||
protected $year; | ||
|
||
/** | ||
* @ManyToOne(targetEntity="Maker") | ||
* @JoinColumn(name="maker_id", referencedColumnName="id", unique=false) | ||
*/ | ||
protected $maker; | ||
|
||
public function getId() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove any code not related with the test There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you mean the getter/setters? |
||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getName() | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function setName($name) | ||
{ | ||
$this->name = $name; | ||
} | ||
|
||
public function getYear() | ||
{ | ||
return $this->year; | ||
} | ||
|
||
public function setYear($year) | ||
{ | ||
$this->year = $year; | ||
} | ||
|
||
public function getMaker() | ||
{ | ||
return $this->maker; | ||
} | ||
|
||
public function setMaker(Maker $maker) | ||
{ | ||
$this->maker = $maker; | ||
} | ||
} | ||
|
||
/** | ||
* @Table() | ||
* @Entity() | ||
*/ | ||
class Maker | ||
{ | ||
/** | ||
* @Id | ||
* @GeneratedValue(strategy="IDENTITY") | ||
* @Column(type="integer", nullable=false) | ||
*/ | ||
protected $id; | ||
|
||
/** | ||
* @Column(type="string") | ||
*/ | ||
protected $name; | ||
|
||
public function getId() | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getName() | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function setName($name) | ||
{ | ||
$this->name = $name; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the reason for the failure on your side is that the ORM defines its own index (with a generated name) for the association.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, now one caveat to this that I can't seem to write a test for atm, is I've actually got an instance on a server where the hash doctrine generated is different. That table was created with schema tool on server A and I'm running update on server B, not sure if that's a factor?
I guess my bigger question is, if I define the index - why would the ORM auto-define it and override my definition?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stanlemon the problem is probably in the ORM's schema tool, as it automatically sets indexes for associations, without allowing them to be renamed.
@deeky666 are there any known issues on DBAL-side about this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if I understand the naming problem but the only factor for possible different auto-generated index names should be the max identifier length of the underlying platform. Otherwise the index names should always be generated the same for a table name and the column names of the index.
@Ocramius is right and the schema tool will currently indeed first create indexes for all associations before actually creating those manually defined. See here. Theoretically the creation of manually defined indexes would have to be moved at the top so that those index definitions will win over the auto-generated ones from the association mappings. But I am not sure if this change would break other things...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@deeky666 I just want to make sure I follow, please forgive me if I'm restating the obvious.
The automatically generated index for the join is processed first, causing this line https://github.com/doctrine/dbal/blob/0d99f152573c2da1a4884cc90a9fdfc038f47f7b/lib/Doctrine/DBAL/Schema/Table.php#L494 to return true on the
@Table
index, thus skipping over the one I've written by hand, correct?This makes sense to me for the
createSchema()
issue, but I'm still puzzled as to why Doctrine wants to regenerate the existing index on the table inupdateSchema()
I would have thought the same line and ultimately this https://github.com/doctrine/dbal/blob/0d99f152573c2da1a4884cc90a9fdfc038f47f7b/lib/Doctrine/DBAL/Schema/Index.php#L201 would have returned true and skipping over generating a new index for it.