【问题标题】:Laravel 5.3 migration doesn't create tablesLaravel 5.3 迁移不会创建表
【发布时间】:2017-05-07 09:43:30
【问题描述】:

我使用命令 php artisan migrate:make 创建了一些迁移,然后用一些字段填充并保存它。这是全新安装,也是第一次运行迁移。

我运行了 php artisan migrate 并且迁移成功完成。然而,虽然迁移表 IS 创建了,并且它有一行包含文件名和批次 1,但没有表。

这是我的迁移文件代码:

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

class CreateFuelLocationsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        //
        Schema::create('fuel_locations', function (Blueprint $table) {
            $table->increments('id');
            $table->string('uid');
            $table->string('name');
            $table->string('fuel_type');
            $table->string('email');
            $table->string('street');
            $table->string('city');
            $table->string('state');
            $table->string('zip');
            $table->string('phone');
            $table->string('service_hours');
            $table->string('payment_methods');
            $table->string('payment_method_other');
            $table->decimal('latitude', 3, 7);
            $table->decimal('longitude', 3, 7);
        });
    }

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

还有几行来自我的 config/database.php:

    'mysql' => [
        'driver' => 'mysql',
        'database' => 'mydb',
        'host' => 'localhost',
        'username' => 'root',
        'password' => '',
        'charset'   => env('DB_CHARSET', 'utf8'),
        'collation' => env('DB_COLLATION', 'utf8_unicode_ci'),
        'prefix'    => env('DB_PREFIX', ''),
        'timezone'  => env('DB_TIMEZONE', '+00:00'),
        'strict'    => env('DB_STRICT_MODE', false),
    ],

我确实尝试将主机更改为 127.0.0.1,但无法连接。我怎样才能修复它,以便它确实像它应该的那样创建表。

【问题讨论】:

  • 您用来检查数据库的任何工具是否有可能没有以某种方式刷新并且表实际上存在?尝试运行 sql show tables; 并查看它是否显示已创建的表。如果迁移表报告它是实际创建的,它应该在那里。
  • 我希望是这样,但我使用的是直接命令行 mysql,根本没有缓存。

标签: php laravel laravel-5.3 artisan-migrate


【解决方案1】:

问题在于以下几行:

$table->decimal('latitude', 3, 7);
$table->decimal('longitude', 3, 7);

您应该会收到类似于以下内容的异常

[PDOException] SQLSTATE[42000]:语法错误或访问冲突:1427 对于 float(M,D)、double(M,D) 或 decimal(M,D),M 必须 >= D(列 '纬度')。

当您进行迁移时。

改为如下

$table->decimal('latitude', 10, 7);
$table->decimal('longitude', 10, 7);

它应该可以工作。

Numeric precision refers to the maximum number of digits that are present in the number

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-28
    • 2021-06-29
    • 2017-02-26
    • 1970-01-01
    • 2013-09-26
    • 2017-07-02
    • 2015-11-24
    • 2017-05-27
    相关资源
    最近更新 更多