简体   繁体   English

如何使用OctoberCMS迁移在现有的完整表中添加新的NOT NULL列

[英]How to add a new NOT NULL column in a existing full table using OctoberCMS migrations

I have a table with a simple structure (id, name and timestamps). 我有一个具有简单结构(id,名称和时间戳)的表。

I need to add a slug but the system is already running and I need the slug in the database to be Unique and NOT NULL (therefore forbidding me to use a default value). 我需要添加一个段,但是系统已经在运行,并且我需要数据库中的段是唯一且不为空(因此禁止我使用默认值)。

How I add a migration file to add this column and fill with with something similar to slug(name) in the Migration file ? 如何添加迁移文件以添加此列并在迁移文件中填充类似于slug(name)的内容?

public function up() {
    Schema::table('departments', function(Blueprint $table) {
        $table->string('slug');
    });
}

In your migration you may specify a default value for the column by doing: 在迁移中,您可以通过执行以下操作为列指定默认值:

// ...
$table->string('slug')->default('my-default-slug');
// ...

This will effectively set my-default-slug as value for every record that already exists inside departments at the time you run your migration. 这将有效地将my-default-slug为运行迁移时departments内部已经存在的每条记录的值。 However, as far as I know does this setting not prevent modifications to any record where the slug is being set to an empty string afterwards. 但是,据我所知,此设置不会阻止对子段之后被设置为空字符串的任何记录进行修改。 That's the whole thing with strings, an empty string is still a valid string to your database. 这就是字符串的全部内容,空字符串仍然是数据库的有效字符串。

See the Laravel docs for more info on column modifiers. 有关列修饰符的更多信息,请参见Laravel文档

EDIT 编辑

In response to the comment below, there is a fine package available for automatic slug generation based on another attribute of an Eloquent model. 根据下面的评论,基于Eloquent模型的另一个属性,有一个很好的程序包可用于自动生成段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM