使用Laravel Eloquent ORM无法向PostgreSQL插入数据

4

在发布帖子之前,我做了很多研究。

使用Laravel eloquent ORM向PostgreSQL插入记录时,我遇到了以下错误:

这是错误信息:

Illuminate \ Database \ QueryException

SQLSTATE[42703]: Undefined column: 
7 ERROR: column "id" does not exist LINE 1: ...ed_at", "created_at") 
values ($1, $2, $3, $4) 
returning "id" ^ (SQL: insert into "fb_post_like_counts" ("post_id", "count", "updated_at", "created_at") values (293843523498_10152007721598499, 0, 2014-03-12 16:56:24, 2014-03-12 16:56:24) returning "id")

这是数据库表创建模式:
Schema::create('fb_post_shares_counts', function($table)
{
    //
    $table->string('post_id');
    $table->string('count')->default(0);

    //  created_at updated_at deleted_at
    $table->timestamps();
    $table->softDeletes();

    // set composite keys   
    $table->primary(array('post_id', 'updated_at'));
});

以下是我正在尝试执行的代码:

// SAVE POST SHARES
$post_share_count   =   new FbPostShareCount;
$post_share_count->post_id   =  $response['id'];
$post_share_count->count    =   $fql_post['share_count'];       
$post_share_count->save();

我创建了一个模型类 FbPostShareCount 并继承自 Eloquent。

我知道 Laravel 不支持复杂键,但在使用 MySQL 时并没有出现这个问题。


1
等待反馈。问题解决了吗? - Seiji Manoan
3个回答

13

将您的FbPostShareCount模型中的主键设置为

class FbPostShareCount extends Eloquent {

    protected $primaryKey = 'post_id';

    ...
}

同样的错误。仅供记录,我在数据库中使用了复合键,我附上了模式。 - Mo Fetch
据我所知,Eloquent目前不支持复合键。 - greatwitenorth
@MoFetch Antonio Carlos Ribeiro 是正确的。但是,正如 arcsum 所说,应该将 INTEGER 作为主键的 ID。 - Seiji Manoan

4

Laravel的Eloquent ORM具有自动递增功能,因此需要将primaryKey设置为null并将increment设置为false。

protected $primaryKey = null;

public $incrementing = false;

1

你尝试过使用正确的数据类型来表示帖子ID吗?

$table->integer('post_id');

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接