phinx icon indicating copy to clipboard operation
phinx copied to clipboard

method hasIndex returns false always for composit index

Open anwar-gazi opened this issue 10 years ago • 6 comments

indexes created with, ALTER TABLE my_table ADD CONSTRAINT sampleindex UNIQUE (field1,field2)

now inside a migrations 'up' method, $Table = $this->table('my_table'); $Table->hasIndex('sampleindex');// returns false

anwar-gazi avatar Dec 22 '15 08:12 anwar-gazi

Try using hasIndexByName('sampleindex') instead.

mwdhouse avatar Jan 04 '16 18:01 mwdhouse

MySQL?

wandersonwhcr avatar Jan 05 '16 18:01 wandersonwhcr

Yeah On Jan 6, 2016 12:24 AM, "Wanderson Camargo" [email protected] wrote:

MySQL?

— Reply to this email directly or view it on GitHub https://github.com/robmorgan/phinx/issues/728#issuecomment-169088529.

anwar-gazi avatar Jan 05 '16 18:01 anwar-gazi

UTSL, method hasIndex must receive columns, not the name of the index.

So, $Table->hasIndex(['field1', 'field2']) should work.

wandersonwhcr avatar Jan 05 '16 18:01 wandersonwhcr

Yeah I'm sorry for bad info; I should've looked closer :(

hasIndexByName is an adapter method, so you have to run it on the table's adapter.

$table = $this->table('my_table');
$exists = $table->getAdapter()->hasIndexByName($table->getName(), 'my_index_name');

mwdhouse avatar Jan 06 '16 17:01 mwdhouse

I run into the exact same thing. Its confusing. Perhaps hasIndex should throw an exception like ~"column not found" or similar.

Then you 1st cannot accidentally provide an index name and 2nd cannot accidentally provide non-existing columns (f.e on typo).

Thanks, /cottton

EDIT or|or pls add the ::hasIndexByName to the \Phinx\Db\Table like

// \Phinx\Db\Table
    public function hasIndexByName($name)
        return $this->getAdapter()->hasIndexByName($this->getName(), $name);

cottton avatar May 02 '17 18:05 cottton