-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
The primary key columns don't have to be in the same order as the table columns #516
Conversation
Hello, thank you for creating this pull request. I have automatically opened an issue http://www.doctrine-project.org/jira/browse/DBAL-793 We use Jira to track the state of pull requests and the versions they got |
@bartv2 seems like Can you also add a test that ensures the order of the keys is kept? |
…er as the table columns
Hmm, this change depends on sqlite version 3.7.16. See http://www.sqlite.org/changes.html#version_3_7_16 |
@bartv2 @Ocramius I would not add a version check in the schema manager, this seems an ugly hack. I think we had something like this in PostgreSQL and removed it again. |
Agree dekyy On 31/01/2014, at 20.26, "Steve Müller" notifications@github.com wrote: @bartv2 https://github.com/bartv2 @Ocramius Reply to this email directly or view it on |
Sort by SQLite's PK index number of course, not array key. |
Take the column order into account and sort non PK columns last
How is this, all the tests pass |
if ($a['pk'] == $b['pk']) { | ||
return $a['cid'] - $b['cid']; | ||
} | ||
if ($a['pk'] == "0" && $b['pk'] != "0") { |
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.
Are this conditional and the one at line 176 required?
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.
You're right they are not, it 0 columns location doesn't matter
{ | ||
$version = \SQLite3::version(); | ||
if(version_compare($version['versionString'], '3.7.16', '<')) { | ||
$this->markTestSkipped('This version of sqlite doesn\'t return the order of the Primary Key.'); |
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.
What does that mean? We get a notice in the SqliteSchemaManager
because some keys (pk or cid) do not exist? Or does the usort work as well.
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.
@beberlei The test only works on SQLite versions > 3.7.16 because PK columns order information was added for the PRAGMA TABLE_INFO()
statement there. Before that it is not possible to tell in which order the columns in a PK were defined when reverse engineering with PRAGMA TABLE_INFO()
.
Therefore this test will fail for earlier versions as it retrieves array('id', 'other_id')
instead of array('other_id', 'id')
.
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'm not quite happy with this version constraint. I see the limitation for earlier SQLite versions but IMO we should at least document this limitation in the known vendors issues. I don't want to see any complaints about this issue from users using a SQLite version before this fix.
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 am not sure @beberlei's comment has been addressed. He is referring to the changes in lib/Doctrine/DBAL/Schema/SqliteSchemaManager.php
. If the pk
column is not there on older Sqlite versions, the usort($indexArray, function($a, $b) {
call will probably throw notices for undefined array index.
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.
@bantu The pk
column is there, it's just that it is always 1
on older versions of SQLite and there you cannot determine the order of columns in a composite PK.
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.
Okay, thanks. So the column is there but contains incorrect values in some cases. Maybe this patch can be accepted now.
Anything stopping this from merging? |
other_id INTEGER, | ||
PRIMARY KEY(other_id, id) | ||
) | ||
EOS |
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.
@bartv2 Is this explicit raw SQL necessary? Can't we just $this->_sm->createTable($table)
here?
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.
No that function will call Schema\Table->getColumns() which returns a sorted list of columns and that is not what we want here.
Besides some CS issues and maybe mentioning this particular limitation in the known vendor issues, this PR is okay from my point of view. |
On older SQLite versions the schema could be dumped which should also contain the correct order of primary key columns. |
Yeah travis seems to have problems sometimes bringing up the PostgreSQL service. Restarted the failing build and it should be okay now. |
The primary key columns don't have to be in the same order as the table columns
@beberlei Can we get this backported to 2.3 please? |
This happens when the columns are created in a different order then in the PK. This doesn't happen with Doctrine, as the order is taken from the PK.