laravel5.2 使用PostgreSQL数据库,主键不为id问题

说明

由于客户要求项目必须使用PostgreSQL数据库,所以项目没有使用mysql数据库,使用laravel5.2开发由于项目是和第三方数据对接的,所以数据库表的id字段无法使用int自增,因此字段类型使用的是varchar(32)类型

问题

主键不是int类型,使用Eloquent ORM 查询数据一对多数据查询为空

表设计情况

一个学校拥有多个教师的情况:学校表


Schema::create('schools', function (Blueprint $table) {
    $table->string('id',32)->comment('学校id');
    $table->string('school_name')->comment('学校名称');
    $table->timestamps();
});

Model关系定义


<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class School extends Model
{
    protected $fillable = ['id','school_name'];
    public function teachers()
    {
        return $this->hasMany('App\Models\Teacher');
    }
}

教师表

Schema::create('teachers', function (Blueprint $table) {
    $table->string('id',32)->comment('教师id');
    $table->string('teacher_name')->comment('教师名称');
    $table->string('school_id',64)->nullable()->comment('学校id');
    $table->timestamps();
});

Model关系定义

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Teacher extends Model
{
    protected $fillable = ['id','teacher_name','school_id'];

    public function school()
    {
        return $this->belongsTo('App\Models\School');
    }
}

现有数据

pingjia=# select * from schools;

+---------------------+--------------+---------------------+---------------------+

| id                | school_name     | created_at    | updated_at          |

+---------------------+--------------+---------------------+---------------------+

| CNCSYS1600000000001 | 广州演示学校 | 2017-08-03 15:59:16 | 2017-08-09 18:16:38 |

| CNCSYS1600000000000 | 演示学校一   | 2017-08-03 11:22:17 | 2017-08-09 18:51:31 |

+---------------------+--------------+---------------------+---------------------+

2 rows in set

pingjia=# 

    
pingjia=# select * from teachers;
+---------------------+--------------+---------------------+---------------------+---------------------+
| id                  | teacher_name | school_id           | created_at          | updated_at          |
+---------------------+--------------+---------------------+---------------------+---------------------+
| CNCSYS1600000000000 | admin        | CNCSYS1600000000000 | 2017-08-03 11:24:12 | 2017-08-03 11:24:17 |
| CNCSYS1600000000001 | wk           | CNCSYS1600000000000 | 2017-08-03 11:50:27 | 2017-08-03 11:50:27 |
| CNCSYS1600000000003 | 教师01       | CNCSYS1600000000001 | 2017-08-03 16:13:21 | 2017-08-08 17:53:43 |
| CNCSYS1600000000020 | 张三         | CNCSYS1600000000000 | 2017-08-07 14:49:28 | 2017-08-07 14:49:28 |
| CNCSYS1600000000026 | 演示教师     | CNCSYS1600000000001 | 2017-08-08 17:57:35 | 2017-08-08 17:57:35 |
| CNCSYS1600000000002 | qingdao XXX  | CNCSYS1600000000000 | 2017-08-03 11:57:37 | 2017-08-08 12:05:34 |
| CNCSYS1600000000136 | 教师02       | CNCSYS1600000000000 | 2017-08-09 16:06:11 | 2017-08-09 16:06:11 |
| CNCSYS1600000000138 | 教师04       | CNCSYS1600000000000 | 2017-08-09 16:06:11 | 2017-08-09 16:06:11 |
| CNCSYS1600000000137 | 教师03       | CNCSYS1600000000000 | 2017-08-09 16:06:11 | 2017-08-09 16:06:11 |
| CNCSYS1600000000139 | 教师05       | CNCSYS1600000000000 | 2017-08-09 16:06:11 | 2017-08-09 16:06:11 |
| CNCSYS1600000000135 | 教师01       | CNCSYS1600000000000 | 2017-08-09 16:06:11 | 2017-08-09 16:06:11 |
+---------------------+--------------+---------------------+---------------------+---------------------+
11 rows in set

pingjia=# 


 进入tinker查询查看学校下所有教师数据查询不到

    >>> namespace App\Models;
    => null
    >>> School::find('CNCSYS1600000000001')->teachers()->get()
    => Illuminate\Database\Eloquent\Collection {#718
         all: [],
       }
    >>>

反之可以查到学校信息

    >>> Teacher::find('CNCSYS1600000000003')->school
    => App\Models\School {#725
         id: "CNCSYS1600000000001",
         school_name: "广州演示学校",
         created_at: "2017-08-03 15:59:16",
         updated_at: "2017-08-09 18:16:38",
       }
    >>>

请问大神们如何解决

先试试这个:

public $incrementing=false;

这个写在 Model 文件里面

Augus
修改的评论也不能少于六个字哦!
JellyBool
修改的评论也不能少于六个字哦!
爱哪无忧又 回复 JellyBool
修改的评论也不能少于六个字哦!