Factory Girl
Factory Girl

Reputation: 907

YII migrations and by default values for table columns

public function up(){

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

This is the up function for migration. As u see it is query for creating new table. By default YII creates default value for timestamp column equal to CURRENT_TIMESTAMP and crates additional parameter and sets it equal to ON UPDATE CURRENT_TIMESTAMP.

I do not need current value for timestamp and i do not need to update this column on updating row. What i must to do? By the way, u use MySQL

Upvotes: 5

Views: 11562

Answers (3)

leitasat
leitasat

Reputation: 916

For google users like me: Yii2 now has it out of the box.

Now you can add ->defaultExpression('CURRENT_TIMESTAMP') to your definition.

Upvotes: 6

tread
tread

Reputation: 11108

Leverage off the MySQL Create Table Script:

show create table tablename

Which gives:

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

Now Add that to the migration:

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

Upvotes: 1

Agrest
Agrest

Reputation: 234

You have to set other default value, null for example

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',
        ));
}

Upvotes: -1

Related Questions