{"action":"created","issue":{"url":"https://api.github.com/repos/Raizlabs/DBFlow/issues/1490","repository_url":"https://api.github.com/repos/Raizlabs/DBFlow","labels_url":"https://api.github.com/repos/Raizlabs/DBFlow/issues/1490/labels{/name}","comments_url":"https://api.github.com/repos/Raizlabs/DBFlow/issues/1490/comments","events_url":"https://api.github.com/repos/Raizlabs/DBFlow/issues/1490/events","html_url":"https://github.com/Raizlabs/DBFlow/issues/1490","id":281254398,"number":1490,"title":"AutoIncrement triggered by 0 value id.","user":{"login":"thornley-touchstar","id":8317388,"avatar_url":"https://avatars2.githubusercontent.com/u/8317388?v=4","gravatar_id":"","url":"https://api.github.com/users/thornley-touchstar","html_url":"https://github.com/thornley-touchstar","followers_url":"https://api.github.com/users/thornley-touchstar/followers","following_url":"https://api.github.com/users/thornley-touchstar/following{/other_user}","gists_url":"https://api.github.com/users/thornley-touchstar/gists{/gist_id}","starred_url":"https://api.github.com/users/thornley-touchstar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/thornley-touchstar/subscriptions","organizations_url":"https://api.github.com/users/thornley-touchstar/orgs","repos_url":"https://api.github.com/users/thornley-touchstar/repos","events_url":"https://api.github.com/users/thornley-touchstar/events{/privacy}","received_events_url":"https://api.github.com/users/thornley-touchstar/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2017-12-12T04:31:14Z","updated_at":"2017-12-18T23:28:13Z","closed_at":null,"author_association":"NONE","body":"DBFlow Version: 4.1.2\r\nIssue Kind (Bug, Question, Feature): Question\r\n\r\nDescription: \r\n\r\nRegarding auto increment support, I noticed that you are using '0' as a signifier for triggering an object's id column to increment automatically. If the id value is predefined in the model/object it must be > 0 to prevent the incrementation.\r\n\r\nThis behaviour seems to be defined specifically in the ModelAdapter<> in the method \"hasAutoIncrement\" which might be better called something like \"requiresAutoIncrement\" with the logic negated, as it is really determining if the column is not predefined and needs to be be auto incremented. I also noticed the method treats null with an exception, which leads me to my question...\r\n\r\nWhat do you think about changing this behaviour so that (rather than 0) if you (optionally?) autoincremented this based on the id being null? The point is that zero is technically a valid \"predefined\" id, and my particular use case actually requires this. I discovered this after finding that all predefined rows that I insert (for data sync) which contain a preset id of '0' got autoincremented, unexpectedly.\r\n\r\nI think this might be better handled as you transition the codebase to Kotlin as you should be able to better detect whether or not the id type is Int? or Int. But it could be addressed either way via annotations...\r\n\r\nJust curious what your thoughts are regarding this, and if my understanding is in error. I am happy to help contribute a solution if you agree. \r\n\r\nThanks in advance! What a great library this is :)"},"comment":{"url":"https://api.github.com/repos/Raizlabs/DBFlow/issues/comments/352590916","html_url":"https://github.com/Raizlabs/DBFlow/issues/1490#issuecomment-352590916","issue_url":"https://api.github.com/repos/Raizlabs/DBFlow/issues/1490","id":352590916,"user":{"login":"thornley-touchstar","id":8317388,"avatar_url":"https://avatars2.githubusercontent.com/u/8317388?v=4","gravatar_id":"","url":"https://api.github.com/users/thornley-touchstar","html_url":"https://github.com/thornley-touchstar","followers_url":"https://api.github.com/users/thornley-touchstar/followers","following_url":"https://api.github.com/users/thornley-touchstar/following{/other_user}","gists_url":"https://api.github.com/users/thornley-touchstar/gists{/gist_id}","starred_url":"https://api.github.com/users/thornley-touchstar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/thornley-touchstar/subscriptions","organizations_url":"https://api.github.com/users/thornley-touchstar/orgs","repos_url":"https://api.github.com/users/thornley-touchstar/repos","events_url":"https://api.github.com/users/thornley-touchstar/events{/privacy}","received_events_url":"https://api.github.com/users/thornley-touchstar/received_events","type":"User","site_admin":false},"created_at":"2017-12-18T23:28:13Z","updated_at":"2017-12-18T23:28:13Z","author_association":"NONE","body":"This was more about allowing the column (if marked auto-incrementing) to be seeded with data that contains a 0, sqlite does not forbid this. You are correct it does semantically default to a starting id of 1, which is fine in this scenario and probably the reason why DBFlow works this way (there isn't much choice).\r\n\r\nThe use case for this is data sync, so say for example I import a table from a backend data service that has the id's pre-generated, but we also allow for additional rows to be added internally (using the auto-increment feature. The backend generated data can contain a valid primary key id that is zero, which would get inserted to the database in DBFlow as an auto incremented id (and not explicitly zero). This obviously causes issues when FK's are depending on the id of 0. I just figured it would be cleaner to allow for nulls, however it doesn't seem to be the case in DBFlow due to the hasAutoIncrement() method in ModelAdapter<>: -\r\n\r\n```\r\n    public boolean hasAutoIncrement(TModel model) {\r\n        Number id = getAutoIncrementingId(model);\r\n        //noinspection ConstantConditions\r\n        if (id == null) {\r\n            throw new IllegalStateException(\"An autoincrementing column field cannot be null.\");\r\n        }\r\n        return id.longValue() > 0;\r\n    }\r\n\r\n```\r\n\r\nI might be missing something as I am a newbie with DBFlow but I couldn't get it to work due to the method above. I didn't use any annotations, and just to clarify I am using Kotlin which the preprocessor warning you pointed out says simply 'don't make it nullable' ;)\r\n\r\nTo be honest with you, to work around this we simply managed the id's internally and don't use the auto-increment feature at this stage, however if DBFlow were to allow using \"null\" as the flag as opposed to \"0\", this might be a better approach as at the sql level, primary keys are typically by definition not-null.\r\n\r\nThis might be something to think about supporting formally with the shift to Kotlin."}}
{"actor":{"display_login":"thornley-touchstar"}}