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 时间序列LSTM模型归回预测代码问题
  • ¥50 使用CUDA如何高效的做并行化处理,是否可以多个分段同时进行匹配计算处理?目前数据传输速度有些慢,如何提高速度,使用gdrcopy是否可行?请给出具体意见。
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),如何通过环岛的原理及完整代码
  • ¥20 机器学习或深度学习问题?困扰了我一个世纪,晚来天欲雪,能饮一杯无?
  • ¥15 c语言数据结构高铁订票系统
  • ¥15 关于wkernell.PDB加载的问题,如何解决?(语言-c#|开发工具-vscode)
  • ¥15 (标签-STM32|关键词-智能小车)
  • ¥20 关于#stm32#的问题,请各位专家解答!
  • ¥15 (标签-python)
  • ¥20 搭建awx,试了很多版本都有错