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

Disabling sequence lookups in models.

I currently have a table:

namespace Models;

use Phalcon\Mvc\Model\Validator\Uniqueness;

/**
  * User
  *
  * Represents a user
  *
  */

 class User extends \Phalcon\Mvc\Model {

    /**
     * @Primary
     * @Identity
     * @Column(type="integer", nullable=false)
     */
    public $id;

    /**
    * @Column(type="string", length=70, nullable=false)
    */
    public $username;

The problem is that the user table is inherited from another schema (foo) that I don't want the database user having access to and the underlying sequence is in foo. I have tried removing @Primary, @Identity. even all of the $id declaration and no matter what, I see a lastInsertId call :

PHP message: SQLSTATE[42501]: Insufficient privilege: 7 ERROR:  permission denied for schema core
#0 [internal function]: PDO->lastInsertId(foo.user_id_se...')
#1 [internal function]: Phalcon\Db\Adapter\Pdo->lastInsertId('foo.user_id_se...')


2.9k
edited Nov '14

I've also tried :

//$user->validation();
$q = "INSERT INTO \Models\User (email, username, password, active, user_type, api_id, private_key) VALUES (:email:, :username:, :password:, :active:, :user_type:, :api_id:, :private_key:)";

$this->modelsManager->executeQuery($q, $user);

With same results:

PHP message: SQLSTATE[42P01]: Undefined table: 7 ERROR:  relation "web.user_id_seq" does not exist
#0 [internal function]: PDO->lastInsertId('web.user_id_seq')


2.9k

class postgresqlpdo extends \Phalcon\Db\Adapter\Pdo\Postgresql { public function supportSequences() { return false; } }

I was able to correct this by extending the postgresql class and overriding the supportSequences var. This relevant bit was in model.c :

            /** 
             * Recover the last "insert id" and assign it to the object
             */
            PHALCON_CALL_METHOD(&last_insert_id, connection, "lastinsertid", sequence_name);
            phalcon_update_property_zval_zval(this_ptr, attribute_field, last_insert_id TSRMLS_CC);

            /** 
             * Since the primary key was modified, we delete the _uniqueParams to force any
             * future update to re-build the primary key
             */
            phalcon_update_property_null(this_ptr, SL("_uniqueParams") TSRMLS_CC);

I am not sure that any change is needed other than extending the class, I was working with an existing skel of mvc micro and missed that this could be overridden. It's also given me second thoughts on the need for serials at all, so wins all around.