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条)

相关推荐 更多相似问题