We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

can not insert null id for autoincrement column

I still with this error in 2.0.10, does anybody know any solution?

is the same as here, but they did not gave solution https://forum.phalcon.io/discussion/6230/$obj%3Esave-==-false-return-serial-is-required#C30269

environment, php 5.6, postgresql 9.5

CREATE TABLE events (
    id SERIAL not null,
    "name" varchar(100) NOT NULL,
    CONSTRAINT events_pkey PRIMARY KEY (id)
)

My sequence is really created:

# select nextval('events_id_seq'::regclass);
 nextval
---------
       2
(1 row)

my post

POST /api/v1/events HTTP/1.1
Host: docker
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: 72e91b3e-e841-623a-f668-4bb6db2bcf53

{ 
    "name": "Douglas"

}

Model

class Events extends ModelBase
{

    public $id;

    public $name;

}

Controller


    function createAction() {

        $event = new Events();
        $post = $this->request->getJsonRawBody();

        $event->name = $post->name;

        if ($event->save() == false) {
            /** @var Message $message */
            foreach ($event->getMessages() as $message) {
                $errors[$message->getField()] = $message->getMessage();
            }
            return $errors;
        }

        return $event->toArray();
    }

output

{
  "id": "id is required"
}

Have you maybe tried using this?

$event->id = new \Phalcon\Db\RawValue("DEFAULT VALUE");

Otherwise remove the not null from your create statement (auto increment fields shouldn't be able to accept null values in practice):

CREATE TABLE events (
    id SERIAL,
    "name" varchar(100) NOT NULL,
    CONSTRAINT events_pkey PRIMARY KEY (id)
)
edited Feb '16

Didn't work both solutions, i'm still trying, even without specifiying, i would like to unset this column before the id, so it will work.

        $event->id = new \Phalcon\Db\RawValue("NEXTVAL('events_id_seq')");
SQLSTATE[22P02]: Invalid text representation: 7 ERROR:  invalid input syntax for integer: "NEXTVAL('events_id_seq')"
<br>#0 [internal function]: PDOStatement-&gt;execute()
<br />
#1 [internal function]: Phalcon\Db\Adapter\Pdo-&gt;executePrepared(Object(PDOStatement), Array, Array)
<br />
#2 [internal function]: Phalcon\Db\Adapter\Pdo-&gt;query('SELECT COUNT(*)...', Array, Array)
<br />
#3 [internal function]: Phalcon\Db\Adapter-&gt;fetchOne('SELECT COUNT(*)...', NULL, Array, Array)
<br />
#4 [internal function]: Phalcon\Mvc\Model-&gt;_exists(Object(Phalcon\Mvc\Model\MetaData\Files), Object(Phalcon\Db\Adapter\Pdo\Postgresql), 'events')
<br />
#5 /var/www/app/controllers/Api/V1/EventsController.php(27): Phalcon\Mvc\Model-&gt;save()
<br />
#6 [internal function]: Controllers\Api\V1\EventsController-&gt;createAction()
<br />
#7 [internal function]: Phalcon\Dispatcher-&gt;dispatch()
<br />
#8 /var/www/public/index.php(33): Phalcon\Mvc\Application-&gt;handle()
<br />
#9 {main}

Also tried with get sequence name didn't work.

    public function getSequenceName()
    {
        return "events_id_seq";
    }

I found the issue, i was using cache for my models, i just cleaned my cache and it works with the new configurations

$di->set('modelsMetadata', function () use ($config) {
    return new MetaDataAdapter(array(
        'metaDataDir' => $config->application->cacheDir . 'metaData/'
    ));
});


1.4k
Accepted
answer
edited Feb '16

I found the issue, i was using cache for my models, i just cleaned my cache and it works with the new configurations

$di->set('modelsMetadata', function () use ($config) {
    return new MetaDataAdapter(array(
        'metaDataDir' => $config->application->cacheDir . 'metaData/'
    ));
});

In the end my table:

CREATE TABLE events (
    id      SERIAL,
    name    VARCHAR(100) NOT NULL,
    CONSTRAINT events_pkey PRIMARY KEY (id)
);

controller


function createAction() {

        $event = new Events();
        $post = $this->request->getJsonRawBody();
        $event->name = $post->name;

        if ($event->save() == false) {
            /** @var Message $message */
            foreach ($event->getMessages() as $message) {
                $errors[$message->getField()] = $message->getMessage();
            }
            return $errors;
        }

        return $event->toArray();
    }

model

class Events extends ModelBase
{

    public $id;

    public $name;

}