{"action":"created","issue":{"url":"https://api.github.com/repos/phalcon/cphalcon/issues/2039","labels_url":"https://api.github.com/repos/phalcon/cphalcon/issues/2039/labels{/name}","comments_url":"https://api.github.com/repos/phalcon/cphalcon/issues/2039/comments","events_url":"https://api.github.com/repos/phalcon/cphalcon/issues/2039/events","html_url":"https://github.com/phalcon/cphalcon/issues/2039","id":27462691,"number":2039,"title":"ORM - Save NULL foreign keys","user":{"login":"calinrada","id":873482,"avatar_url":"https://avatars.githubusercontent.com/u/873482?v=3","gravatar_id":"","url":"https://api.github.com/users/calinrada","html_url":"https://github.com/calinrada","followers_url":"https://api.github.com/users/calinrada/followers","following_url":"https://api.github.com/users/calinrada/following{/other_user}","gists_url":"https://api.github.com/users/calinrada/gists{/gist_id}","starred_url":"https://api.github.com/users/calinrada/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/calinrada/subscriptions","organizations_url":"https://api.github.com/users/calinrada/orgs","repos_url":"https://api.github.com/users/calinrada/repos","events_url":"https://api.github.com/users/calinrada/events{/privacy}","received_events_url":"https://api.github.com/users/calinrada/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":35,"created_at":"2014-02-12T19:40:24Z","updated_at":"2015-04-10T18:04:44Z","closed_at":null,"body":"I have a table with a froiegn key (scan_id) that can be null. In the model i have: \r\n\r\n```php\r\n    public function beforeValidation()\r\n    {\r\n        if ($this->scan_id == '') {\r\n            $this->scan_id = new \\Phalcon\\Db\\RawValue('NULL');\r\n        }\r\n    }\r\n```\r\n\r\nBut when i try to save the object i get \"Value of field \"scan_id\" does not exist on referenced table\". \r\n\r\nAny suggestions ? Thanks.\r\n\r\nPhalcon 1.2.6\r\nPHP 5.5.8\r\n\r\n\r\n<bountysource-plugin>\r\n---\r\nWant to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/1675589-orm-save-null-foreign-keys?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/91638019","html_url":"https://github.com/phalcon/cphalcon/issues/2039#issuecomment-91638019","issue_url":"https://api.github.com/repos/phalcon/cphalcon/issues/2039","id":91638019,"user":{"login":"GBraL","id":6546199,"avatar_url":"https://avatars.githubusercontent.com/u/6546199?v=3","gravatar_id":"","url":"https://api.github.com/users/GBraL","html_url":"https://github.com/GBraL","followers_url":"https://api.github.com/users/GBraL/followers","following_url":"https://api.github.com/users/GBraL/following{/other_user}","gists_url":"https://api.github.com/users/GBraL/gists{/gist_id}","starred_url":"https://api.github.com/users/GBraL/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/GBraL/subscriptions","organizations_url":"https://api.github.com/users/GBraL/orgs","repos_url":"https://api.github.com/users/GBraL/repos","events_url":"https://api.github.com/users/GBraL/events{/privacy}","received_events_url":"https://api.github.com/users/GBraL/received_events","type":"User","site_admin":false},"created_at":"2015-04-10T18:04:44Z","updated_at":"2015-04-10T18:04:44Z","body":"I thought about it but the problem with this solution is that some use gets setters and with different attribute name.\r\n\r\nBy this I would have to treat each case individually or standardize all my models.\r\nIt is striking that the Phalcon can not handle such a situation.\r\n\r\nI can not insert NULL value in a FK because there is a relationship with the parent table and I can not remove the relationship is not adding any value that does not exist in the child table will not be treated by the application, launching a database exception."}}