无法使用 Laravel eloquent ORM 插入 PostgreSQL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22359003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:10:22  来源:igfitidea点击:

Unable to insert into PostgreSQL using laravel eloquent ORM

phppostgresqllaravellaravel-4eloquent

提问by Mo Fetch

I did my research a lot before posting this.

在发布这篇文章之前,我做了很多研究。

I am getting the following error when trying to insert a record into PostgreSQL using Laravel eloquent ORM.

尝试使用 Laravel eloquent ORM 将记录插入 PostgreSQL 时出现以下错误。

This is the error:

这是错误:

Illuminate \ Database \ QueryException

SQLSTATE[42703]: Undefined column: 
7 ERROR: column "id" does not exist LINE 1: ...ed_at", "created_at") 
values (, , , ) 
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")

This is the database table create schema:

这是数据库表创建模式:

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'));
});

and this is the code i am trying to execute:

这是我要执行的代码:

// 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();

and I created a model class FbPostShareCount extends Eloquent.

我创建了一个模型类 FbPostShareCount 扩展了 Eloquent。

I know that laravel does not support complex keys but this problem did not occur when I was using MySQL

我知道laravel不支持复杂的键但是我在使用MySQL时没有出现这个问题

回答by Antonio Carlos Ribeiro

Set the primary key in your FbPostShareCountmodel as

FbPostShareCount模型中的主键设置为

class FbPostShareCount extends Eloquent {

    protected $primaryKey = 'post_id';

    ...
}

回答by arcsum

have you tried using the correct data type for post id?

您是否尝试过为帖子 ID 使用正确的数据类型?

$table->integer('post_id');

回答by Chando

Laravel eloquent ORM have auto increment, So set up primaryKey = null and increment = false.

Laravel eloquent ORM 有自动增量,所以设置primaryKey = null 和increment = false。

protected $primaryKey = null;

public $incrementing = false;