【问题标题】:Unable to insert into PostgreSQL using laravel eloquent ORM无法使用 laravel eloquent ORM 插入 PostgreSQL
【发布时间】:2014-04-17 00:42:03
【问题描述】:

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

尝试使用 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 的时候没有出现这个问题

【问题讨论】:

  • 等待反馈。解决了吗?

标签: php postgresql laravel laravel-4 eloquent


【解决方案1】:

FbPostShareCount 模型中的主键设置为

class FbPostShareCount extends Eloquent {

    protected $primaryKey = 'post_id';

    ...
}

【讨论】:

  • 同样的错误。只是为了记录,我在附加架构的数据库中使用复合键。
  • 据我所知,eloquent 目前不支持复合键。
  • @MoFetch Antonio Carlos Ribeiro 是对的。但是,正如arcsum 也说过,它应该是INTEGER 作为PK 的ID。
【解决方案2】:

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

protected $primaryKey = null;

public $incrementing = false;

【讨论】:

    【解决方案3】:

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

    $table->integer('post_id');
    

    【讨论】:

      猜你喜欢
      • 2019-08-01
      • 2016-03-16
      • 2012-09-24
      • 2012-12-10
      • 2013-01-22
      • 2021-07-03
      • 2019-05-25
      • 2017-07-07
      • 2018-01-09
      相关资源
      最近更新 更多