迁移时出错:应该删除列的回滚 - 使用Laravel Homestead& SQlite的

时间:2016-10-31 17:25:56

标签: laravel-5 sqlite composer-php

我在运行应该删除列的“php artisan migrate:rollback”时遇到错误。

我已经跑了:
作曲家需要学说/ dbal
作曲家更新
我甚至试过“composer dump-autoload”

当我使用“sqlite3 database / database.sqlite”检查数据库模式时,该表包含所有列(包括一个名为“excerpt”的列,这是应该删除的流氓列)但是在运行回滚时得到列未发布的错误。见下文。

(环境:laravel 5.3- Homestead,Gitbash,Sublime Text3,SQlite3)

错误如下:

[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1 no such column: published (SQL: CREATE TEMPORARY TABLE __temp__articles AS SELECT id, title, body, created_at, updated_at, published at FROM articles)

[Doctrine\DBAL\Driver\PDOException]
SQLSTATE[HY000]: General error: 1 no such column: published

[PDOException]
SQLSTATE[HY000]: General error: 1 no such column: published

我的架构如下:

CREATE TABLE "migrations" ("id" integer not null primary key autoincrement, "migration" varchar not null, "batch" integer not null);
CREATE TABLE "users" ("id" integer not null primary key autoincrement, "name" varchar not null, "email" varchar not null, "password" varchar not null, "remember_token" varchar null, "created_at" datetime null, "updated_at" datetime null);
CREATE UNIQUE INDEX "users_email_unique" on "users" ("email");
CREATE TABLE "password_resets" ("email" varchar not null, "token" varchar not null, "created_at" datetime null);
CREATE INDEX "password_resets_email_index" on "password_resets" ("email");
CREATE INDEX "password_resets_token_index" on "password_resets" ("token");
CREATE TABLE "articles" ("id" integer not null primary key autoincrement, "title" varchar not null, "body" text not null, "created_at" datetime null, "updated_at" datetime null, "published at" datetime not null, "excerpt" text null);

创建列的迁移如下:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddExcerptToArticlesTable extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::table('articles', function (Blueprint $table) {

        $table->text("excerpt")->nullable();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::table('articles', function (Blueprint $table) {

        $table->dropColumn("excerpt");

    });
}
}

如何解决这个问题,以便您可以使用migrate:rollback?

无缝删除列

1 个答案:

答案 0 :(得分:0)

我使用了一个在hometead虚拟盒子里运行完美的mysql数据库。记住:

  1. 登录宅基地:“mysql -u homestead -p”(在下一行提示输入密码时输入“secret” - 它被屏蔽)

  2. 创建自己的数据库:“create database your_database_name;”

  3. 并将其设置为.env文件中的数据库名称,例如:

    DB_CONNECTION=mysql                                                       
    DB_HOST=127.0.0.1
    DB_PORT=3306 
    DB_DATABASE=your_database_name
    DB_USERNAME=homestead
    DB_PASSWORD=secret 
    

    喔。当您创建文章迁移时,请进行如下的一些更改:

    $表 - &GT;时间戳( “publishedat”) - &GT;可为空的();

    //注意我已经为定义的时间戳添加了nullable,这将使迁移工作,否则你将在运行php artisan migrate时遇到以下错误

    [Illuminate\Database\QueryException]
    SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'publishedat' (SQL: create table `articles` (`id` int unsigned not null auto_increment primary key, `title` varchar(255) not null, `body` text not null, `created_at` timestamp null, `updated_at` timestamp null, `publishedat` timestamp not null) default character set utf8 collate utf8_unicode_ci)
    

    //注意我已经删除了发布时的空白,只读了已发布的内容(published_at也是另一种选择);当你进入Eloquent阶段时,这将为你节省大量的悲伤,你的文章对象根本不会保存();因此不会致力于数据库。

    如果您正在使用Git Bash,您可以在此处“https://gist.github.com/hofmannsven/9164408”查看一些有用的命令,例如查看数据库表等。