douya2007 2017-08-22 05:00
浏览 59
已采纳

在laravel中添加引用外键的表时,如何修复错误?

(2/2) QueryException SQLSTATE[42S22]: Column not found: 1054 Unknown column 'productdesc_id' in 'field list' (SQL: insert into subSalesPackage (salesPackage, productdesc_id, updated_at, created_at) values (, 0, 2017-08-22 04:45:24, 2017-08-22 04:45:24))

This is the error occurs, on adding the table.

I have two table ProductDescription(table1) and subSalesPackage(table2). And here table1 has a primary key field as descid and also foreign key field as product_id referred from another table.

table2 has a primary key field as id and foreign key field as product_descid referred from the table1(ie.,descid).

But, when I am trying to add the table2 values, it shows error(mentioned @top)

Model for ProductDescription:

   <?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class productDescription extends Model
{
//
 protected $table="ProductDescription";
protected $connection="mysql";

 public function productPricing()
 {

        return $this->belongsTo(priceInfo::class);
 }
 **public function salesPackage()
 {
        return $this->hasMany(packageModel::class);
 }**
 }

Model2->for subSalesPackage

   <?php

  namespace App;

use Illuminate\Database\Eloquent\Model;

  class packageModel extends Model
 {
//
 protected $table="subSalesPackage";
 protected $connection="mysql";

  public function productdesc(){

        return $this->belongsTo('App\productDescription');
    }
    }

Controller:

public function addProductDetails(Request $formdescription,$dataId)
{
  $description=new productDescription;
  $description->deviceCategoryId=$dataId;
  $description->descid=$this->getproductDescriptionId();
  $description->skillSet =$formdescription->input('skillSet');
  $description->rechargable=$formdescription->input('rechargable');
  //$product->productPricing()-save($priceInfo); 
  //$product->productDetails()->save($description);
  $description->save();

  $salesPackage=new packageModel;
  $salesPackage->salesPackage=$formdescription->input('package');
  $salesPackage->productdesc()->associate($description);
  //$salesPackage->product_description()->associate($description);
  $salesPackage->save();


 return response()->json([
    'modelName'    => $formdescription->mname,
    'colour' => $formdescription->colour,
    'rechargable' => $formdescription->rechargable,
    'batteryType' => $formdescription->batteryType
]);


 }
 This is the controller code I have tried for adding both the tables.

Schema-table1:

    public function up()
{
    //
    Schema::create('ProductDescription', function (Blueprint $table) {
        $table->engine='InnoDB';
        $table->string('descid')->primary();
        $table->string('product_id');
        $table->string('deviceCategoryId');
        $table->string('modelName');
         $table->string('Height');
         $table->string('rechargable');
         $table->foreign('product_id')->references('id')-
  >on('productPriceDetails')->onUpdate('cascade')-
  >onDelete('cascade');
        $table->timestamps();  
        $table->index(['descid']);  

    });
}

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

Schema-table2:

    public function up()
   {
    //
    Schema::create('subSalesPackage', function (Blueprint $table) {
        $table->increments('id');
        $table->string('product_descid');
        $table->string('salesPackage');
        $table->foreign('product_descid')->references('descid')-
        >on('ProductDescription')->onDelete('cascade')
        ->onUpdate('cascade');
        $table->timestamps();  
        $table->index(['id']);  

    });
    }

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    //
    Schema::dropIfExists('subSalesPackage');
}
  • 写回答

2条回答 默认 最新

  • dongyan6235 2017-08-22 06:44
    关注

    You need to rename product_descid to productdesc_id in subSalesPackage table and check.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵