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 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable