{"action":"created","issue":{"url":"https://api.github.com/repos/phalcon/cphalcon/issues/1134","labels_url":"https://api.github.com/repos/phalcon/cphalcon/issues/1134/labels{/name}","comments_url":"https://api.github.com/repos/phalcon/cphalcon/issues/1134/comments","events_url":"https://api.github.com/repos/phalcon/cphalcon/issues/1134/events","html_url":"https://github.com/phalcon/cphalcon/issues/1134","id":18426316,"number":1134,"title":"[BUG] New record, PostgreSQL and the Primary key","user":{"login":"tema87","id":1525524,"avatar_url":"https://avatars.githubusercontent.com/u/1525524?v=3","gravatar_id":"","url":"https://api.github.com/users/tema87","html_url":"https://github.com/tema87","followers_url":"https://api.github.com/users/tema87/followers","following_url":"https://api.github.com/users/tema87/following{/other_user}","gists_url":"https://api.github.com/users/tema87/gists{/gist_id}","starred_url":"https://api.github.com/users/tema87/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tema87/subscriptions","organizations_url":"https://api.github.com/users/tema87/orgs","repos_url":"https://api.github.com/users/tema87/repos","events_url":"https://api.github.com/users/tema87/events{/privacy}","received_events_url":"https://api.github.com/users/tema87/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/phalcon/cphalcon/labels/Bug%20-%20Medium","name":"Bug - Medium","color":"d7e102"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":12,"created_at":"2013-08-22T17:03:53Z","updated_at":"2015-10-18T07:11:54Z","closed_at":null,"body":"Test table in postgres:\r\n```sql\r\nCREATE TABLE robots\r\n(\r\n  id serial NOT NULL,\r\n  name character varying,\r\n  CONSTRAINT robots_pkey PRIMARY KEY (id)\r\n);\r\n```\r\n\r\nModel:\r\n```php\r\nclass Robot extends \\Phalcon\\Mvc\\Model\r\n{\r\n    public function getSource()\r\n    {\r\n        return 'robots';\r\n    }\r\n}\r\n```\r\n\r\nScenario №1:\r\n```php\r\n$robot = new Robot();\r\n\r\n$robot->name = uniqid();\r\n\r\n$robot->save();\r\n```\r\nresult: OK\r\n\r\n\r\nScenario №2\r\n```php\r\n$robot = new Robot();\r\n\r\n$robot->id = 666; // predefined primary key\r\n$robot->name = uniqid();\r\n\r\n$robot->save();\r\n```\r\nresult: \"PDOException: SQLSTATE[55000]: Object not in prerequisite state: 7 ERROR: currval of sequence \"robots_id_seq\" is not yet defined in this session\"\r\n\r\n\r\nScenario №3\r\n```php\r\n$robot = new Robot();\r\n\r\n$robot->id = $robot->getReadConnection()->fetchOne(\"SELECT NEXTVAL('robots_id_seq') as id\")['id']; // predefined primary key from sequence\r\n$robot->name = uniqid();\r\n\r\n$robot->save(); //OK\r\n//but:\r\n$robot->refresh();\r\n```\r\nresult: \"Phalcon\\Mvc\\Model\\Exception: The record cannot be refreshed because it does not exist or is deleted\"\r\n\r\n<bountysource-plugin>\r\n---\r\nWant to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/1896447-bug-new-record-postgresql-and-the-primary-key?utm_campaign=plugin&utm_content=tracker%2F50707&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F50707&utm_medium=issues&utm_source=github).\r\n</bountysource-plugin>"},"comment":{"url":"https://api.github.com/repos/phalcon/cphalcon/issues/comments/148986274","html_url":"https://github.com/phalcon/cphalcon/issues/1134#issuecomment-148986274","issue_url":"https://api.github.com/repos/phalcon/cphalcon/issues/1134","id":148986274,"user":{"login":"MikkelPaulson","id":585944,"avatar_url":"https://avatars.githubusercontent.com/u/585944?v=3","gravatar_id":"","url":"https://api.github.com/users/MikkelPaulson","html_url":"https://github.com/MikkelPaulson","followers_url":"https://api.github.com/users/MikkelPaulson/followers","following_url":"https://api.github.com/users/MikkelPaulson/following{/other_user}","gists_url":"https://api.github.com/users/MikkelPaulson/gists{/gist_id}","starred_url":"https://api.github.com/users/MikkelPaulson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MikkelPaulson/subscriptions","organizations_url":"https://api.github.com/users/MikkelPaulson/orgs","repos_url":"https://api.github.com/users/MikkelPaulson/repos","events_url":"https://api.github.com/users/MikkelPaulson/events{/privacy}","received_events_url":"https://api.github.com/users/MikkelPaulson/received_events","type":"User","site_admin":false},"created_at":"2015-10-18T07:11:54Z","updated_at":"2015-10-18T07:11:54Z","body":"MUCH better workaround: put this in your model.\r\n\r\n    public static function getSequenceName() {\r\n        return null;\r\n    }\r\n\r\nBam!"}}