Laravel迁移-在表中添加检查约束 [英] Laravel Migration - Adding Check Constraints In Table

查看:68
本文介绍了Laravel迁移-在表中添加检查约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想像这样在Laravel Migration中创建一个表-

I want to create a table in Laravel Migration like this-

CREATE TABLE Payroll
(
 ID int PRIMARY KEY, 
 PositionID INT,
 Salary decimal(9,2) 
 CHECK (Salary < 150000.00)
);

我所做的是-

Schema::create('Payroll', function (Blueprint $table)
{
    $table->increments('id');
    $table->integer('PositionID ');
    $table->decimal('Salary',9,2);
    //$table->timestamps();
});

但是我不能创建这个

 CHECK (Salary < 150000.00)

任何人都可以告诉我们如何在 Laravel迁移中实现此CHECK约束吗?

Can anyone please tell, how to implement this CHECK constraints in Laravel Migration ?

推荐答案

Blueprint类不支持添加约束(至少从Laravel 5.3起),但是 可以向其中添加约束通过使用数据库语句直接从迁移中创建表.

Adding constraints is not supported by the Blueprint class (at least as of Laravel 5.3), however it is possible to add constraints to your tables directly from your migrations, by using database statements.

在您的迁移文件中,

public function up ()
{
    Schema::create('payroll', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('position_id');
        $table->decimal('salary',9,2);
    });

    // Add the constraint
    DB::statement('ALTER TABLE payroll ADD CONSTRAINT chk_salary_amount CHECK (salary < 150000.00);');
}

这篇关于Laravel迁移-在表中添加检查约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆