Skip to content
Advertisement

how to make a varchar nullable and unique together in mysql (laravel)

Can i make a MySQL column nullable and unique together. I have a table that store users Email_id if user wants to provide else it will be (null). I read in some other questions that i can make a unique field with default NULL. but i get this error when creating table

JavaScript

the main table is generated with larave schema builder class

JavaScript

in DB

JavaScript

now error is

JavaScript

Now question is how to handle a varchar field which is unique and nullable together.

Advertisement

Answer

I know this is an old question, this is for others who have the same issue.

You are trying to add an empty string. You made column nullable and unique. That means you are trying something you shouldn’t. You can send multiple null values but not mutiple empty string. MySQL consider empty as a value.

or you could write a mutator in your modal

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement