【问题标题】:SQLSTATE[HY000]: General error: 1 table posts has no column named *SQLSTATE[HY000]:一般错误:1 个表帖子没有名为 * 的列
【发布时间】:2020-08-22 06:44:05
【问题描述】:

我实际上是 Laravel 的新手,我正在尝试使用这个框架构建一个基本的社交网络。对于这个项目,我创建了一个名为 post 的页面,用户可以在其中添加新帖子。所以我尝试像这样创建posts 表:

public function up()
{
    Schema::create('posts', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->unsignedBigInteger('user_id');
        $table->text('caption');
        $table->string('image');
        $table->timestamps();
        $table->index('user_id');
    });
}

User.php 模型上:

public function posts()
{
    return $this->hasMany(Post::class);
}

还有扩展模型的Post.php

class Post extends Model
{
    protected $guarded = [];

    Public function user()
    {
        return $this->belongsTo(User::class);
    }
}

而名为PostsController.php 的控制器是这样的:

class PostsController extends Controller
{
    public function create()
    {
        return view('posts.create');
    }
    public function store()
    {
        $data = request()->validate([
            'caption' => 'required',
            'image' => ['required','image'],
        ]);

        auth()->user()->posts()->create($data);

        dd(request()->all());
    }
}

这是posts文件夹下的create.blade.phpresources目录:

@extends('layouts.app')

@section('content')
<div class="container">
    <form action="/p" enctype="multipart/form-data" method="post">
        @csrf
        <div class="row">
            <div class="col-8 offset-2">
                <div class="row">
                    <h1>Add New Post</h1>
                </div>
                <div class="form-group row">
                    <label for="caption" class="col-md-4 col-form-label">Post Caption</label>

                  
                    <input id="caption" 
                    type="text" 
                    class="form-control @error('caption') is-invalid @enderror" 
                    name="caption" 
                    value="{{ old('caption') }}" 
                    autocomplete="caption" autofocus>

                    @error('caption')
                        <span class="invalid-feedback" role="alert">
                            <strong>{{ $message }}</strong>
                        </span>
                    @enderror
                 
                </div>
                <div class="row">
                    <label for="image" class="col-md-4 col-form-label">Post Image</label> 
                    <input type="file" class="form-control-file" id="image" name="image">
                    @error('image')
                        <strong>{{ $message }}</strong>
                    @enderror
                </div>
                <div class="row pt-4">
                    <button class="btn btn-primary">Add New Post</button>
                </div>
            </div>
        </div>
    </form>
</div>
@endsection

如果你想看看routes.php,这里是:

Auth::routes();

Route::get('/p/create','PostsController@create');
Route::post('/p','PostsController@store'); 

Route::get('/profile/{user}', 'ProfilesController@index')->name('profile.show');

所以一切看起来都很干净,但问题是每当我尝试上传一些带有标题的虚拟图片时,我都会看到这个错误:

SQLSTATE[HY000]: General error: 1 table posts has no column named caption

但是我尝试在 CMD 上运行 php artisan migrate 语法来检查数据库迁移是否遗漏任何内容,它会输出:Nothing to migrate!

因此,如果您知道为什么会出现此问题或如何解决此问题,请告诉我,我将不胜感激!

提前致谢。

【问题讨论】:

    标签: php laravel sqlite


    【解决方案1】:

    在您的迁移中,您需要创建关系,如下所示:

    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('user_id');
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->text('caption');
            $table->string('image');
            $table->timestamps();
            // $table->index('user_id');
        });
    }
    

    错误:您为外键创建了一个$table-&gt;unsignedBigInteger('user_id');
    但是您没有将其定义为具有 relationship外键,您需要添加此行以与 users 表建立关系, $table-&gt;foreign('user_id')-&gt;references('id')-&gt;on('userses')-&gt;onDelete('cascade');

    Laravel 7 增加了Foreign Key Constraints 方法,在这个方法中你可以用一行代码定义一个外键,如:

    $table->foreignId('user_id')->constrained();
    

    这与:

    $table->unsignedBigInteger('user_id');
    $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    

    foreignId 方法是unsignedBigInteger 的别名,而constrained 方法将使用约定来确定所引用的表和列名。如果您的表名不符合约定,您可以通过将表名作为参数传递给受约束的方法来指定表名:

    注意:MySQL自动将foreign key作为index,所以你不需要定义,所以我删除了这一行@ 987654331@你可以找到更多关于here的信息。

    现在,如果您运行 php artisan migrate,您将得到相同的 Nothing to migrate!,因为您已经根据迁移迁移了所有表,并且 laravel 将所有迁移的文件名保存在表中叫migrations。您需要运行php artisan migrate:refresh,此命令将删除您之前的所有表并创建新表。

    【讨论】:

    • 我照你说的做了,但现在我得到了这个错误:SQLSTATE[HY000]: General error: 1 no such table: main.userses (SQL: insert into "posts"
    【解决方案2】:
    class Post extends Model
    {
        protected $guarded = [];
    
        Public function user()
        {
            return $this->belongsTo(User::class,'user_id', 'id');
        }
    }
    

    你可以尝试指定对应的外键,可能你的外键没有指定对,也可以提供你的User's Model看看

    【讨论】:

      【解决方案3】:

      我有同样的问题,我用这个命令修复了它,即使它会删除你数据库中的数据,但它会工作

      php artisan migrate:refresh 
      

      【讨论】:

        【解决方案4】:

        我之前遇到过这个错误,我所做的只是运行这个命令:

        php artisan migrate:refresh 
        

        【讨论】:

        • 我认为这是解决问题的方法,但你能解释一下你为什么这样做吗?类似于:我正在处理我的迁移,但我的数据库没有更新,因为 Laravel 无法知道迁移文件是否已更改......也许
        • 此命令可能会截断您通过模型和控制器定义的所有表不要尝试。数据库结构会很好,但你所有的数据都会消失。如果你在玩,这很好,但如果你有一个生产环境,它很可能会对你的环境构成威胁,稍微说一下。
        猜你喜欢
        • 2012-10-24
        • 2018-06-18
        • 1970-01-01
        • 2012-10-25
        • 2014-12-31
        • 2012-04-29
        • 1970-01-01
        • 2021-09-19
        相关资源
        最近更新 更多