Mehdi's avatar

Eloquent and postgresql incrementing

Hello everyone,

I work on application that uses a database already created with postgresql.

I try to insert a new record in a table, using Eloquent and Query Builder. But I have the following error: Imgur

Id of this database is auto-increment type.

Any ideas !?

0 likes
11 replies
lpadilla's avatar

It looks like is something related to Mass Assignment. How do you model looks like?

Mehdi's avatar

That's my model :


<?php Class EbrizMessenger extends Eloquent{ /** * @var string */ protected $table = 'ebrizmessenger'; /** * @var string */ protected $primaryKey = 'id'; /** * @var bool */ public $timestamps = false; }

That's what i try to do :

$data = [
            'user_code_sender' => $this->user->user_code,
            'broadcasting_category' => $input['broadcasting_category'],
            'priority' => $input['priority'],
            'subject' => $input['subject'],
            'message' => $input['message'],
            'message_sending_date' => date("Y-m-d H:i:s"),
        ];
        if($input['broadcasting_category'] == '2')
        {
            $data['entity_id'] = $this->user->rest_entity_id;
            $data['auto_hide_datetime'] = date("Y-m-d H:i:s", strtotime("+1 week"));
        }

        if($input['broadcasting_category'] == '0')
            $data['user_code_receiver'] = $input['user_code_receiver'];

        \DB::table('ebrizmessenger')->insert($data);
Mehdi's avatar

Or with Eloquent :

$message = new \EbrizMessenger();
$message->user_code_sender = $this->user->user_code;
$message->broadcasting_category = $input['broadcasting_category'];

$message->priority = $input['priority'];
$message->subject = $input['subject'];
$message->message = $input['message'];
$message->message_sending_date = date("Y-m-d H:i:s");
$message->save()
lpadilla's avatar

Did you read the link i posted here? To be simple you need to specify fields inside model something like this:

Class EbrizMessenger extends Eloquent{
    protected $fillable = ['priority', 'subject', 'message'];
}
Mehdi's avatar

I have already tried :

/**
* @var array
*/
protected $guarded = array('id');

But still the same error !

lpadilla's avatar

Can you show me the structure of your table? With $fillable should be enough, but you need to specify fields required, obviously.

Mehdi's avatar
Mehdi
OP
Best Answer
Level 1

Thank's @lpadilla for your reply.

The probleme was, for whatever reason, the id can't be generated/incremented by postgresql.

So I creat a function who do this stuff :

'sq_statementcont_seq' is the name of my sequencer for this table.


/** * @return int */ protected function getNextStatementId() { $next_id = \DB::select("select nextval('sq_statementcont_seq')"); return intval($next_id['0']->nextval); }
2 likes
drewgallagher's avatar

@Mehdi I am currently experiencing this issue with our laravel php unit tests. The seeders are inserting data without incrementing the sequence. Did you ever encounter this issue for unit tests and resolve it?

Do you have to reset the sequence for every single table that you insert into?

tobz.nz's avatar

I know this is old but thought I'd post in case it helps someone else(or my future self) I just had this issue and descovered that one of my seeders was manually setting the id value. Removing them fixed the issue.

For some reason if the primary key is specified in the data input, then Postgres does not update the auto-increment value.

smatana's avatar

@tobz.nz

Hello,

yes for me it is always after .csv import so after every manual import for fixing I am pasting max statement DB::statement('SELECT setval('machines_id_seq', (SELECT MAX(id) FROM machines))');

Please or to participate in this conversation.