Welcome Guest, Not a member yet? Register   Sign In
How can I remove a unique constraint on a field when performing a down migration?
#1

Copy-pasted from https://stackoverflow.com/questions/6503...-migration, feel free to answer the question there too to win a few points.

I have to add a unique constraint to a field after the table was created. I therefore set up a migration file with this in the up() method:
Code:
$fields = [
    'slug' => [
        'type' => 'varchar',
        'constraint' => 255,
        'unique' => true
    ]
];
$this->forge->modifyColumn('pages', $fields);
And in down():
Code:
$fields = [
    'slug' => [
        'type' => 'varchar',
        'constraint' => 255,
        'unique' => false
    ]
];
$this->forge->modifyColumn('pages', $fields);
This doesn't work.

When calling
Code:
php spark migrate:rollback
, the migration is correctly un-applied ("migrations" table is modified) but the fields stay unique.
What should I put in down() instead?
Reply
#2

I don't think that you can without deleting the field and adding it in again
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

You could execute some SQL to delete it directly:


PHP Code:
public function down()
{
  
    $this
->db->simpleQuery("ALTER TABLE pages DROP INDEX slug");




(the unique index will have a name - if you don't know what it is, you can get it with this: SHOW INDEX FROM pagesWink
Reply




Theme © iAndrew 2016 - Forum software by © MyBB