LaravelでRedshiftにインサートする際にでるSQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "returning"の対処法
どうも、くずきです。
以前、Laravel
でRedshift
を扱う方法について書きました。
今回は、Redshift
にインサートする際にでる
SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "returning"
の対処法について。
EloquentとRedshiftを紐づける
以前の記事ではコードかかなかったけど、Eloquent
にはモデルごとにコネクションを変えることができる。
class User extends Model { protected $connection = 'redshift'; protected $table = 'users'; }
$connection
に該当する接続先を定義してあげれば良い。
Redshiftにインサートする
冒頭に述べたように、Eloquent
ではRedshift
に接続した際に、そのままインサートするとエラーがでる。
User::create(...)
上記のようにcreate
する場合、Eloquent
ではデフォルトで、最後にインサートしたid
を返すクエリーを投げている。
SQL: insert into "user" ("user_id", "updated_at", "created_at") values (335767, 2017-12-28 15:54:04, 2017-12-28 15:54:04) returning "id"
returning id
がその部分だ。
これの解消方法は簡単で、
class User extends Model { // 2つ追加 protected $primaryKey = null; public $incrementing = false; protected $connection = 'redshift'; protected $table = 'users'; }
primaryKey
をnull
にし、incrementing
をfalse
にしてauto increment
の設定をオフにすれば良い。