yii2数据表迁移命令:yii migrate/create 文件名

yii2.0 / 432人浏览 / 0人评论

yii migrate,使用yii migrate命令创建数据库表迁移文件命令:yii migrate/create create_blog_table,如下:结果我们可以找到这样一个文件:/console/migrations/m211110_131127_create_blog_table.php,这个文件中你可以这样修改:类中有两个方法,其实你看得懂,表字段是自己新增的。

yii migrate

使用yii migrate命令创建数据库表迁移文件命令:

yii migrate/create create_blog_table

如下

结果我们可以找到这样一个文件:

/console/migrations/m211110_131127_create_blog_table.php

这个文件中你可以这样修改:

类中有两个方法,其实你看得懂,表字段是自己新增的。

<?php

use yii\db\Migration;

/**
 * Handles the creation of table `{{%blog}}`.
 */
class m211110_131127_create_blog_table extends Migration
{
    /**
     * {@inheritdoc}
     */
    public function safeUp()
    {
        $this->createTable('{{%blog}}', [
            'id' => $this->primaryKey(),
            'title' => $this->string(100)->notNull()->defaultValue(''),
            'content' => $this->text(),
            'create_time' => $this->datetime(),
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function safeDown()
    {
        $this->dropTable('{{%blog}}');
    }
}

生成对应的表:

完成以上步骤(接下来在数据库中生成对应的表),使用命令:

./yii migrate

以下是(以上说明)命令执行,生成表过程

[root@..................com]# yii migrate/create create_blog_table
Yii Migration Tool (based on Yii v2.0.43)
Error: SQLSTATE[HY000] [2002] Connection refused
[root@..................com] yii migrate/create create_blog_table
Yii Migration Tool (based on Yii v2.0.43)

Error: SQLSTATE[HY000] [2002] Connection refused
[root@..................com] yii migrate/create create_blog_table
Yii Migration Tool (based on Yii v2.0.43)

Create new migration '................/console/migrations/m211110_131127_create_blog_table.php'? (yes|no) [no]:yes
New migration created successfully.
[root@..................com] ./yii migrate
Yii Migration Tool (based on Yii v2.0.43)

Creating migration history table "migration"...Done.
Total 3 new migrations to be applied:
        m130524_201442_init
        m190124_110200_add_verification_token_column_to_user_table
        m211110_131127_create_blog_table

Apply the above migrations? (yes|no) [no]:yes
*** applying m130524_201442_init
    > create table {{%user}} ... done (time: 0.026s)
*** applied m130524_201442_init (time: 0.058s)

*** applying m190124_110200_add_verification_token_column_to_user_table
    > add column verification_token string NULL DEFAULT NULL to table {{%user}} ... done (time: 0.037s)
*** applied m190124_110200_add_verification_token_column_to_user_table (time: 0.041s)

*** applying m211110_131127_create_blog_table
    > create table {{%blog}} ... done (time: 0.011s)
*** applied m211110_131127_create_blog_table (time: 0.015s)

3 migrations were applied.

Migrated up successfully.

如下图:

navicat

转载注明:

扩展查找

0 条评论

还没有人发表评论

发表评论 取消回复

记住我的信息,方便下次评论
有人回复时邮件通知我