当前位置: 首页 > 知识库问答 >
问题:

Laravel迁移不会在DB中添加表

惠翰藻
2023-03-14

我在文件2018\u 02\u 08\u 094356\u create\u currency\u table.php中的迁移中添加了一个新表。。

这是代码:

<?php

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

class CreateCurrencyTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('currency', function (Blueprint $table) {
            $table->increments('id');
            $table->decimal('usdeur', 15, 2);
            $table->decimal('usdchf', 15, 2);
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('currency');
    }
}

当我运行php artisan migrate时,我的数据库中只有默认的laravel用户(和迁移)表。没有货币。

原因可能是什么?

编辑

迁移一切都很顺利。问题是:

SQLSTATE[42000]:语法错误或访问冲突:1071指定的密钥太长;最大密钥长度为767字节(SQL:更改表用户添加唯一users_email_unique(电子邮件))

SQLSTATE[42000]:语法错误或访问冲突:1071指定的密钥太长;最大密钥长度为767字节

解决方案是在AppServiceProvider.php

public function boot()
{
    Schema::defaultStringLength(191);
}

来源:https://laravel-news.com/laravel-5-4-key-too-long-error

共有3个答案

钮誉
2023-03-14

所有迁移记录都记录在表迁移中。这表明您以前创建过它。因此,请删除表迁移中有关货币的记录,然后再次运行php artisan migrate。此外,刷新数据库并检查是否创建了表货币。

饶德元
2023-03-14

打开AppServiceProvider.php编写以下代码:

public function boot()
{
    Schema::defaultStringLength(191);
}

表名必须为复数,以便使用回滚迁移

php artisan migrate:rollback

手动从数据库和迁移表中删除该表,并尝试以下代码:

migration command:
php artisan make:migration create_currencies_table

代码:

class CreateCurrenciesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('currencies', function (Blueprint $table) {
            $table->increments('id');
            $table->decimal('usdeur', 15, 2);
            $table->decimal('usdchf', 15, 2);
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('currencies');
    }
}

现在快跑

php artisan migrate
陆昕
2023-03-14

您需要先运行composer du来注册迁移类。

然后运行php artisan migrate命令执行新的迁移。

 类似资料:
  • 我花了一整天的时间试图弄清楚下面的迁移发生了什么。 这是表的迁移。我的想法是使用“id_stays_abroad”和“user_id”作为外键。 这是添加外键的另一个迁移 当我运行php artisan迁移时,我遇到了以下错误: SQLSTATE[HY000]:一般错误:1215无法添加外键约束(SQL:alter table

  • 当我们在Laravel框架中运行migrate:install时,migrations表是在有两列(migration,batch)的数据库中创建的,无论如何都要添加在时间戳处创建的新列。谢谢

  • 我想在laravel中现有的表中添加一些新列。 我已经在谷歌上搜索过了,在这些搜索之后,我已经使用命令创建了迁移。 将列添加到用户。php 创建之后,我运行这个命令。 但也犯了同样的错误: 基本表或视图已存在:1050个表“用户”已存在(SQL:create table(int unsigned not null自动递增主键,varchar(255)not null,varchar(255)not

  • 我不知道如何使用Laravel框架将新列添加到我现有的数据库表中。 我尝试使用...编辑迁移文件。 在终端中,我执行和。 如何添加新列?

  • 我创建了一个名为用户的表。有一些表被称为公司、名称、部门。我想添加company_id,designation_id,department_id列到用户表作为外键。 我试过了,但没用 当我迁移迁移时,它会显示此错误。 Illumb\Database\QueryException:SQLSTATE[HY000]:一般错误:1005无法创建表(错误号:150“外键约束格式不正确”)(SQL:alter

  • 我有一些表,我想在这个表中添加新的列。我想添加默认值为空的字符串列。 我试着这样加上去 或 但我有一个错误