-
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
Closed
Closed
Join column index names #1138
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
115 changes: 115 additions & 0 deletions
115
tests/Doctrine/Tests/ORM/Functional/Ticket/JoinColumnIndexNameTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<?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__ . '\JoinColumnIndexNameTest_Car'), | ||
$this->_em->getClassMetadata(__NAMESPACE__ . '\JoinColumnIndexNameTest_Maker'), | ||
)); | ||
|
||
$expected = array ( | ||
0 => 'CREATE TABLE JoinColumnIndexNameTest_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 JoinColumnIndexNameTest_Car (maker_id)', | ||
2 => 'CREATE TABLE JoinColumnIndexNameTest_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__ . '\JoinColumnIndexNameTest_Car'), | ||
$this->_em->getClassMetadata(__NAMESPACE__ . '\JoinColumnIndexNameTest_Maker'), | ||
); | ||
|
||
$schemaTool = new \Doctrine\ORM\Tools\SchemaTool($this->_em); | ||
$schemaTool->createSchema($classes); | ||
|
||
$sql = $schemaTool->getUpdateSchemaSql($classes, true); | ||
|
||
$schema = $schemaTool->getSchemaFromMetadata($classes); | ||
|
||
$carTable = $schema->getTable('JoinColumnIndexNameTest_Car'); | ||
|
||
$indexes = $this->_em->getConnection()->getSchemaManager()->listTableIndexes('JoinColumnIndexNameTest_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 JoinColumnIndexNameTest_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 JoinColumnIndexNameTest_Car | ||
{ | ||
/** | ||
* @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="JoinColumnIndexNameTest_Maker") | ||
* @JoinColumn(name="maker_id", referencedColumnName="id", unique=false) | ||
*/ | ||
protected $maker; | ||
} | ||
|
||
/** | ||
* @Table() | ||
* @Entity() | ||
*/ | ||
class JoinColumnIndexNameTest_Maker | ||
{ | ||
/** | ||
* @Id | ||
* @GeneratedValue(strategy="IDENTITY") | ||
* @Column(type="integer", nullable=false) | ||
*/ | ||
protected $id; | ||
|
||
/** | ||
* @Column(type="string") | ||
*/ | ||
protected $name; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.