之前为了配置方便,我将配置文件都单独配置到跟application 同级目录的config 文件夹下面了,但是执行迁移命令时间,显示数据库连接不上
There was a problem connecting to the database: SQLSTATE[HY000] [1045]
Access denied for user 'root'@'localhost' (using password: NO)
经过排查才知道是找不到database的原因,然后我将config当中的database 移到application 目录下面才得以解决
<?php
use think\migration\Migrator;
use think\migration\db\Column;
class CreateUsersTable extends Migrator
{
public function up()
{
}
public function change()
{
// 创建表第一个参数为表名
$table = $this->table('users', array('engine' => 'InnoDB'));
// 添加字段 limit->字段的长度 defalut->字段的默认值 comment-> 字段备注 null -> 默认为false,字段不能为空,设置为true 可以让字段为空
$table->addColumn('username', 'string', array('limit' => 100, 'default' => '', 'comment' => '用户名'))
->addColumn('password', 'string', array('limit' => 32, 'default' => md5('123456'), 'comment' => '用户密码'))
->addColumn('email', 'string', array('limit' => 32, 'default' => '', 'comment' => '邮箱', 'null' => true))
->addColumn('avatar', 'string', array('limit' => 32, 'default' => '', 'comment' => '头像', 'null' => true)) //
->addColumn('status', 'integer', array('limit' => 2, 'default' => 0, 'comment' => '账户状态 0-正常 1-已禁用'))
->addColumn('is_delete', 'boolean', array('limit' => 1, 'default' => 0, 'comment' => '删除状态,1已删除'))
// 添加唯一索引
->addIndex(array('email'), array('unique' => true))
// 添加 create_time 与 update_time 两个字段
->addTimestamps()
->create();
}
}