Laravel Eloquent 如何创建具有重复 NULL 的 UNIQUE 约束
问题描述
我使用的是带有 MS Sql Server 2014 的 Laravel 5.我想创建一个唯一约束,但它应该允许多个空值.
I'm usinq Laravel 5 with MS Sql Server 2014. I want to create a unique constraint but it should allow multiple null values.
这是我正在使用的代码.如果 'passport_no' 不为空,则必须是唯一的.
Here is code I'm using. Where 'passport_no' has to be unique if not null.
推荐答案
这是一个古老的问题,但仍然需要回答.如上所述,2008 年的 SQL Server(包括 Azure SQL)支持可以解决它的特殊索引.在您的数据库迁移过程中,您可以检查所使用的驱动程序并将数据库构建器标准 SQL 替换为特定于 MSSQL 的语句.
This is an ancient question, but sill needs answering. As stated above, SQL Server from 2008, including Azure SQL, supports a special index that will work around it. In your database migration you can check the driver used and substitute the database builder standard SQL with an MSSQL-specific statement.
此迁移示例适用于 Laravel 5+,并创建一个具有唯一但可为空的 api_token
列的用户表:
This migration example is for Laravel 5+, and creates a users table with a unique, but nullable, api_token
column:
这篇关于Laravel Eloquent 如何创建具有重复 NULL 的 UNIQUE 约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!