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 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形