【问题标题】:Laravel Foreign Key is changed to lowercaseLaravel 外键改为小写
【发布时间】:2015-09-13 20:18:36
【问题描述】:

我正在尝试访问关系字段值,当我尝试执行此操作时,出现错误:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'sub_areas.subarea_id' in 'where clause' (SQL: select * fromsub_areaswheresub_areas.subarea_idis null limit 1)

我不明白为什么要把它改成带下划线的小键;

感谢任何帮助。

访问模型:

$subArea = City::find(1)->subArea;

在模型中,主键是:

城市模型:

/**
* Overriding default Primary Key
* @var string
*/
protected $primaryKey = 'SubAreaID';

public function subArea()
{
    return $this->belongsTo(SubArea::class);
}

子区域模型

public function city()
{
     return $this->hasMany(City::class);
}

迁移:

     Schema::create('cities', function (Blueprint $table) {

        $table->engine = 'InnoDB';

        $table->increments('Id');
        $table->integer('AreaID')->unsigned();
        $table->integer('SubAreaID')->unsigned();
        $table->integer('CityID')->unsigned()->index();
        $table->string('Title', 50);
        $table->string('Latitude', 100);
        $table->string('Longitude', 100);            
        $table->tinyInteger('Active')->unsigned()->default(1);
    });

添加外键:

   Schema::table('cities', function(Blueprint $table) {
        $table->foreign('AreaID')
              ->references('AreaID')->on('areas')
              ->onDelete('cascade')->onUpdate('restrict');

        $table->foreign('SubAreaID')
              ->references('SubAreaID')->on('sub_areas')
              ->onDelete('cascade')->onUpdate('restrict');    
    });       

如果我像这样在函数内部传递外键

城市模型

  return   $this->hasOne(SubArea::class, 'SubAreaID');

子区域模型

  return $this->belongsToMany(\City::class, 'cities', 'SubAreaID');

我没有得到任何结果。 但这是另一个问题。

【问题讨论】:

  • 您从哪个模型访问关系?你能展示你如何尝试访问关系的代码吗?
  • 我已经编辑了我的问题

标签: php laravel laravel-4 laravel-5


【解决方案1】:

首先,根据您的迁移,您的City 模型的主键不是SubAreaID。只是Id。将其分配给 SubAreaID 实际上会导致 find(1) 查找 SubAreaID 为 1 的列,我不相信这是你想要做的。

现在,默认情况下,当你定义 belongsTo 关系时,Laravel 有一个默认的命名约定。

按照惯例,Eloquent 将采取“蛇案” 拥有模型的名称并以 _id 为后缀。 http://laravel.com/docs/5.1/eloquent-relationships

在这种情况下,当您在City 中定义$this->belongsTo(SubArea::class) 时,它将假定您在City 模型中有一个subarea_id 列。

但是,您可以通过将自定义键作为第二个参数传递来覆盖此行为。

public function subArea()
{
    return $this->belongsTo(SubArea::class, 'SubAreaID');
}

同样,在您的 SubArea 模型中

public function city()
{
     return $this->hasMany(City::class, 'SubAreaID');
}

最后是最后两个关系

城市模型

return   $this->hasOne(SubArea::class, 'SubAreaID');

分区模型

return $this->belongsToMany(\City::class, 'cities', 'SubAreaID');

行不通,因为这根本不是您的表的设置方式。

【讨论】:

  • 谢谢你,这是帮助,为什么我会收到这样的查询? ` 'select * from cities where cities.SubAreaID is null and cities.SubAreaID is not null and cities.Id = ?'`空?
  • @Victorino 这个查询是什么?你什么时候做City::find(1)->subArea;或者什么?
  • 是的。这是我的模型访问-$city = $sub->city()->where('cities.Id', 1)->get()->toArray();,我得到的查询是正确的,除了设置SubAreaID is NULL 这里是select * from cities`,其中cities.SubAreaID 为空,cities.Id = ? ` 我不明白为什么
  • 因为当您执行$sub->city() 时,您试图找到所有具有SubAreaCity。这意味着SubAreaID 不可能为空。如果SubAreaID 为空,则关系无效。
猜你喜欢
  • 1970-01-01
  • 2017-11-24
  • 2017-12-01
  • 2015-11-18
  • 2020-10-16
  • 1970-01-01
  • 2012-03-08
  • 2023-02-06
相关资源
最近更新 更多