Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to insert into PostgreSQL using laravel eloquent ORM

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.

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 ($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")

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.

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

like image 244
Mo Fetch Avatar asked Dec 09 '22 09:12

Mo Fetch


2 Answers

Set the primary key in your FbPostShareCount model as

class FbPostShareCount extends Eloquent {

    protected $primaryKey = 'post_id';

    ...
}
like image 199
Antonio Carlos Ribeiro Avatar answered Dec 11 '22 08:12

Antonio Carlos Ribeiro


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

protected $primaryKey = null;

public $incrementing = false;
like image 31
Chando Avatar answered Dec 11 '22 07:12

Chando