【问题标题】:"SQLSTATE[42000]: Syntax error or access violation: 1103 Incorrect table name error“SQLSTATE[42000]: 语法错误或访问冲突:1103 Incorrect table name error
【发布时间】:2018-02-18 22:46:13
【问题描述】:

我正在尝试使用图像和标签的 id 填充 image_tag 中间表,但是,我不断收到此错误消息,据我了解,即使表名不正确,该表名也不正确。它的 image_id 和 tag_id 列采用无符号整数。

“SQLSTATE[42000]: 语法错误或访问冲突: 1103 不正确的表名'image_tag' (SQL: 插入image_tag (image_id, tag_id) 值(17, 12))”

    public function uploadImage(Request $request){
            $this->validate($request, [
                'name' => 'required|max:120',
                'description' => 'max:120|nullable',
                'image' => 'required|max:1999'
            ]);

            $name = $request['name'];
            $description = $request['description'];
            $tag = $request['tags'];
            $userId = auth()->user()->id;
            $file = $request->file('image')->getClientOriginalName();
            $fileName = pathinfo($file, PATHINFO_FILENAME);
            $extension = $request->file('image')->getClientOriginalExtension();
            $fileNameToStore = $fileName.'_'.time().'.'.$extension;
            $fileNameToStore = str_replace(' ', '', $fileNameToStore);
            $path = $request->file('image')->storeAs('public/images/uploaded', $fileNameToStore);

            $image = new Image();
            $image->name = $name;
            $image->description = $description;
            $image->user_id = $userId;
            $image->file_name = $fileNameToStore;

            $image->save();
            $image->tags()->attach($tag);

            return redirect()->route('home');
    }

图像模型:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Image extends Model
{
    public function user(){
        return $this->belongsTo('App\User');
    }

    public function tags(){
        return $this->belongsToMany('App\Tag', 'image_tag ','image_id','tag_id');
    }
}

标签模型:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Tag extends Model
{
    public function images(){
        return $this->belongsToMany('App\Image', 'image_tag','tag_id','image_id');
    }
}

【问题讨论】:

  • 您是否连接到正确的数据库服务器/数据库?
  • 是的,我可以上传新的标签和图像(当不尝试将它们连接到某个标签时)但是现在我正在尝试填充中间表,我收到了这个错误。
  • 尝试复数为 image_tags 会很有说服力吗?你对image_tag 模型有什么看法吗?
  • 我没有 image_tag 模型。

标签: php laravel


【解决方案1】:

你写的'image_tag'中有一个空格:|

return $this->belongsToMany('App\Tag', 'image_tag ','image_id','tag_id');

【讨论】:

  • 我太瞎了。谢谢。
【解决方案2】:

清除配置缓存并重新运行迁移。

【讨论】:

    猜你喜欢
    • 2015-10-12
    • 2023-04-01
    • 2018-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-24
    相关资源
    最近更新 更多