1

I had data in my database. Now i want to add a column

$table->string('md5_url');

It work. But now i want to add unique index on md5_url. But it don't work because rows in my database have same value(=default value). So what should i do if i want to create new column form existed column(such as i want md5_url = md5(url_column_value)

I have known 2 way to set default value.

$table->string('md5_url')->default($value);

and

$stack_links = DB::table('stack_links')->update(['md5_url' => $value]);

but 2 above way, the $value is fix, how to make it flexible (diffirent in each row) so that i can add unique index after that.

Note: i don't want to create model StackLink so don't try this way:

$stack_links = StackLink::get();
foreach($stack_links as $stack_link) {
   $stack_link->md5_url = md5($stack_link->url);
   $stack_link->save();
}

Update: have try

$table->string('md5_url')->default(uniqid());

but it don't work too.The result is

enter image description here

Ngo Tuan
  • 205
  • 1
  • 2
  • 16

2 Answers2

1

Try IGNORE to set unique index to add new column.

public function up()
{
    Schema::table('listings', function (Blueprint $table) {
        $table->string('md5_url');
    });
    DB::statement('ALTER IGNORE TABLE listings ADD UNIQUE INDEX myindex (md5_url)');
}

public function down()
{
    Schema::table('listings', function (Blueprint $table) {
        $table->dropColumn('md5_url');
    });
}
Sohel0415
  • 9,523
  • 21
  • 30
  • thank you, but it not work . uniqid() only return 1 random value, and all the row in the database get it – Ngo Tuan Jan 09 '18 at 06:49
  • Oh, I see!! then you need to modify your value through some query. – Sohel0415 Jan 09 '18 at 06:52
  • there is already a question similar to yours here https://stackoverflow.com/questions/36647058/removing-duplicates-with-unique-index – Sohel0415 Jan 09 '18 at 07:26
  • no ,if i use ignore the duplicate will be removed. But i don't want to remove it. It only the init value of it. After i set unique index, then i will update it value = md(column_url_value) – Ngo Tuan Jan 09 '18 at 07:31
  • you don't have any value initially, so there is no question of removing duplicate. – Sohel0415 Jan 09 '18 at 07:34
  • At the time a ask this question, i don't have nullable(), so that the value init = ' ' – Ngo Tuan Jan 09 '18 at 07:39
  • Do you really need to keep ' '? – Sohel0415 Jan 09 '18 at 07:41
  • no , as i said, after that i will set it value = md5(url_column_value). But before i set, i need to give it unique index, and values ' ' make me unable to set unique index. Btw, thank u for spending time to help me. I will upvote u . Many thanks! – Ngo Tuan Jan 09 '18 at 07:47
  • sometimes talking helps us to find a better solution, that's what i am trying here too and i think we all have a better solution here on top that works for you and will works for others too. – Sohel0415 Jan 09 '18 at 07:50
  • Yes, i know. There are many way to help me to resolve the problem.So thank for all. But i will choose the simplest way , and that way can do all i need. – Ngo Tuan Jan 09 '18 at 07:52
1

Change your column property like this:

$table->string('md5_url')->nullable();

It's okay now!

Huy Nguyễn
  • 158
  • 1
  • 7