在Laravel迁移中以连字符(-)重命名列名

时间:2019-05-30 14:23:48

标签: php laravel database-migration

我正在尝试运行laravel迁移,以重命名数据库中的特定列,但是我反复收到错误消息。

at Layer.handle [as handle_request] (/rbd/pnpm-volume/f9d011c8-bafe-411e-a616-958a5a1a748d/node_modules/.registry.npmjs.org/express/4.16.4/node_modules/express/lib/router/layer.js:95:5)

at next (/rbd/pnpm-volume/f9d011c8-bafe-411e-a616-958a5a1a748d/node_modules/.registry.npmjs.org/express/4.16.4/node_modules/express/lib/router/route.js:137:13)

at Route.dispatch (/rbd/pnpm-volume/f9d011c8-bafe-411e-a616-958a5a1a748d/node_modules/.registry.npmjs.org/express/4.16.4/node_modules/express/lib/router/route.js:112:3)

at Layer.handle [as handle_request] (/rbd/pnpm-volume/f9d011c8-bafe-411e-a616-958a5a1a748d/node_modules/.registry.npmjs.org/express/4.16.4/node_modules/express/lib/router/layer.js:95:5)

at /rbd/pnpm-volume/f9d011c8-bafe-411e-a616-958a5a1a748d/node_modules/.registry.npmjs.org/express/4.16.4/node_modules/express/lib/router/index.js:281:22

at Function.process_params (/rbd/pnpm-volume/f9d011c8-bafe-411e-a616-958a5a1a748d/node_modules/.registry.npmjs.org/express/4.16.4/node_modules/express/lib/router/index.js:335:12)

at next (/rbd/pnpm-volume/f9d011c8-bafe-411e-a616-958a5a1a748d/node_modules/.registry.npmjs.org/express/4.16.4/node_modules/express/lib/router/index.js:275:10)

at /rbd/pnpm-volume/f9d011c8-bafe-411e-a616-958a5a1a748d/node_modules/.registry.npmjs.org/body-parser/1.19.0/node_modules/body-parser/lib/read.js:130:5

at invokeCallback (/rbd/pnpm-volume/f9d011c8-bafe-411e-a616-958a5a1a748d/node_modules/.registry.npmjs.org/raw-body/2.4.0/node_modules/raw-body/index.js:224:16)

我不断收到此错误消息。

class ModifyColumnName extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('posts', function (Blueprint $table) {
            $table->renameColumn("content-description", "content_description");
        });
    }

}

3 个答案:

答案 0 :(得分:0)

在反引号中使用连字符添加列名

Schema::table('posts', function (Blueprint $table) {
    $table->renameColumn("`content-description`", "content_description");
});

答案 1 :(得分:0)

数据库名称中的连字符不好,连字符是一个大问题,因为如果您最终将列名称映射到变量,则大多数语言都不喜欢在变量名称中使用连字符。

但是您可以将它们与反t招一起使用,只需将列名放在反勾号(`)中

public function up()
{
    Schema::table('posts', function (Blueprint $table) {
        $table->renameColumn("`content-description`", "content_description");
    });
}

答案 2 :(得分:0)

由于该列没有数据,请尝试此操作...

class ModifyColumnName extends Migration
{
 /**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::table('posts', function (Blueprint $table) {
        $table->dropColumn("`content-description`");
        $table->string("`content_description`");
    });
}
/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::table('posts', function (Blueprint $table) {
        $table->string("`content-description`");
        $table->dropColumn("`content_description"`);
    });
}

}

根据mariadb.com:“ 标识符可以使用反引号-进行引用。对于不包含特殊字符的标识符,或者对于非保留字的标识符,如果ANSI_QUOTES SQL_MODE标志,则可以选择使用引号设置后,也可以使用双引号(“)来引用标识符。

这就是为什么我更喜欢innoDB ...!总是使用单引号