{"record":{"id":"b14f5d749b85f265","repo":"greenrobot/greenDAO","slug":"duplicate-name-for","errorCode":null,"errorMessage":"Duplicate name for ","messagePattern":"Duplicate name for ","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"DaoGenerator/src/org/greenrobot/greendao/generator/Entity.java","lineNumber":639,"sourceCode":"            }\n        }\n    }\n\n    void init3rdPass() {\n        for (Property property : properties) {\n            property.init3ndPass();\n        }\n\n        init3rdPassRelations();\n        init3rdPassAdditionalImports();\n    }\n\n    private void init3rdPassRelations() {\n        Set<String> toOneNames = new HashSet<>();\n        for (ToOne toOne : toOneRelations) {\n            toOne.init3ndPass();\n            if (!toOneNames.add(toOne.getName().toLowerCase())) {\n                throw new RuntimeException(\"Duplicate name for \" + toOne);\n            }\n        }\n\n        Set<String> toManyNames = new HashSet<>();\n        for (ToManyBase toMany : toManyRelations) {\n            toMany.init3rdPass();\n            if (toMany instanceof ToMany) {\n                Entity targetEntity = toMany.getTargetEntity();\n                for (Property targetProperty : ((ToMany) toMany).getTargetProperties()) {\n                    if (!targetEntity.propertiesColumns.contains(targetProperty)) {\n                        targetEntity.propertiesColumns.add(targetProperty);\n                    }\n                }\n            }\n            if (!toManyNames.add(toMany.getName().toLowerCase())) {\n                throw new RuntimeException(\"Duplicate name for \" + toMany);\n            }\n        }","sourceCodeStart":621,"sourceCodeEnd":657,"githubUrl":"https://github.com/greenrobot/greenDAO/blob/0bbb338e17c4cf28aba5aae62d42f73f50186157/DaoGenerator/src/org/greenrobot/greendao/generator/Entity.java#L621-L657","documentation":"During the generator's third init pass, init3rdPassRelations collects lowercased to-one relation names in a set and throws a RuntimeException if a name was already seen. Duplicate to-one names would generate clashing getter methods, so the generator fails early with the offending ToOne object in the message.","triggerScenarios":"Calling entity.addToOne(target, fk) twice on the same entity where both ToOne relations resolve to the same generated name (often because the relation name derives from the target entity name, e.g. two to-one relations to the same target entity).","commonSituations":"Two foreign keys pointing to the same target entity (e.g. order.billToAddress and order.shipToAddress both via addToOne(address, ...)) without custom names; copy-pasted relation declarations; loop/tree entities where parent/child both target the same class without naming overrides.","solutions":["Give each to-one relation a unique name where supported, or ensure target entities differ so derived names differ.","Remove one of the duplicate addToOne calls; if two FKs to the same entity are needed, rename the FK properties or use ToMany/projection instead.","Check toOneRelations count per entity during schema setup and assert uniqueness yourself.","Use distinct wrapper entities or manually modeled FK properties for multiple references to the same target type."],"exampleFix":"// before\norder.addToOne(address, billingAddressId); // name \"address\"\norder.addToOne(address, shippingAddressId); // duplicate derived name\n// after\norder.addLongProperty(\"billingAddressId\");\norder.addLongProperty(\"shippingAddressId\");\n// and keep a single addToOne, or model both FKs as plain properties","handlingStrategy":"validation","validationCode":"Set<String> names = new HashSet<>();\n// before generating: assert each entity's to-one targets are distinct\nfor (ToOne t : entity.getToOneRelations()) {\n    if (!names.add(t.getTargetEntity().getClassName().toLowerCase())) {\n        throw new IllegalStateException(\"Duplicate to-one to \" + t.getTargetEntity().getClassName());\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    generateAll(schema);\n} catch (RuntimeException e) {\n    if (e.getMessage().startsWith(\"Duplicate name for\")) {\n        throw new IllegalStateException(\"Two to-one relations resolve to the same name — rename targets or model FKs manually\", e);\n    }\n    throw e;\n}","preventionTips":["Avoid two addToOne calls to the same target entity on one entity","Give relations distinct target entities or use plain FK properties","Count toOneRelations per entity in schema tests","Document multiple-reference cases (billing/shipping) as plain properties"],"tags":["greendao","code-generation","duplicate","relations"],"backgroundTag":"schema-validation-failed","analyzedSha":"0bbb338e17c4cf28aba5aae62d42f73f50186157","analyzedAt":"2026-09-08T03:22:36.050Z","contentChangedAt":"2026-09-08T03:22:36.050Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}