YII迁移和表列的默认值

w46czmvw  于 2022-11-09  发布在  其他
关注(0)|答案(3)|浏览(147)
public function up(){

        $this->createTable('POST', array(
            'id' => 'pk',
            'isremoved' => 'integer NOT NULL',
            'removaldate' => 'timestamp NULL',
            'post' => 'text NOT NULL',
            'creationdate' => 'timestamp NOT NULL',
        ));
}

这是迁移的up函数。正如你所看到的,这是创建新表的查询。默认情况下,YII为时间戳列创建默认值,等于CURRENT_TIMESTAMP,并创建额外的参数,将其设置为ON UPDATE CURRENT_TIMESTAMP。
我不需要时间戳的当前值,也不需要在更新行时更新此列。我必须做什么?顺便说一句,你使用MySQL

6gpjuf90

6gpjuf901#

对于像我这样的谷歌用户:Yii2现在已经开箱即用了。
现在,您可以将->defaultExpression('CURRENT_TIMESTAMP')添加到定义中。

f45qwnt8

f45qwnt82#

利用MySQL创建表脚本:

show create table tablename

其给出:

CREATE TABLE `tablename` (
....
....
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
...

现在将其添加到迁移中:

'timestamp' => 'timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP'
zaq34kh6

zaq34kh63#

您必须设置其他默认值,例如null

public function up(){

        $this->createTable('POST', array(
            'id' => 'pk',
            'isremoved' => 'integer NOT NULL',
            'removaldate' => 'timestamp DEFAULT NULL',
            'post' => 'text NOT NULL',
            'creationdate' => 'timestamp DEFAULT NULL',
        ));
}

相关问题