【问题标题】:Laravel Database Insert Error in artisan tinker工匠修补程序中的 Laravel 数据库插入错误
【发布时间】:2020-11-21 04:53:22
【问题描述】:

The error

Illuminate/Database/QueryException with message 'SQLSTATE[HY000]: General error: 1 table profile has no column named title (SQL: insert into "profiles" ("title", "description", "user_id" , "updated_at", "created_at") 值 (asd, 123123, 2, 2020-07-31 10:19:03, 2020-07-31 10:19:03))'

这是我在 '$profile -> save();' 时遇到的错误 我正在根据以下链接学习 Laravel: https://www.youtube.com/watch?v=ImtZ5yENzgE&list=WL&index=45&t=81s

这是 2020_07_31_074115_create_profiles_table.php

   {
       Schema::create('profiles', function (Blueprint $table) {
           $table->bigIncrements('id');
           $table->unsignedBigInteger('user_id');
           $table->string('title')->nullable();
           $table->text('description')->nullable();
           $table->string('url')->nullable();
           $table->timestamps();

           $table->index('user_id'); //FK
       });
   }

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

这是 2014_10_12_000000_create_users_table.php

<?php

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

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('username')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

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

【问题讨论】:

  • 添加标题列后,您是否迁移过数据库? table profiles has no column named title 表示您已经迁移了表,但没有名为 title 的列。
  • 我在我的项目路径中执行了“php artisan migrate”,但它显示“Nothing to migrate”。或者在我更改 npm run dev 之类的代码或重新打开 cmd 之后有什么事情要做吗?
  • 我的回答解决了你的问题吗?
  • @KurtFriars 是的,先生,您的回答有效。非常感谢

标签: php database laravel sqlite eloquent


【解决方案1】:

如果这是你的本地开发环境,你可以运行:

php artisan migrate:fresh

根据设计,迁移命令对文件中的更改不敏感。它使用文件名来了解它已经运行了哪些迁移,以及需要运行哪些迁移。

如果您已编辑文件,第二次运行 php artisan migrate 将无效。

要进行更改以更改您的生产数据库,您需要进行新的迁移并更改表,而不是编辑旧的迁移文件。

【讨论】:

    猜你喜欢
    • 2016-04-01
    • 2017-08-25
    • 2019-03-05
    • 2018-06-26
    • 2021-03-29
    • 2023-03-17
    • 2016-08-31
    • 2016-03-17
    • 1970-01-01
    相关资源
    最近更新 更多