greenrobot/greenDAO · error · RuntimeException
Target entity has no primary key, but we need it for
Error message
Target entity has no primary key, but we need it for
What it means
ToManyWithJoinEntity.init3rdPass() also requires the target entity of the many-to-many relation to have a primary key, because join-entity rows must reference both sides. If targetEntity.getPropertiesPk() is empty, the generator throws this RuntimeException naming the relation.
Solutions
- Add a primary key to the target entity (e.g. entity.addIdProperty().primaryKey())
- Define a composite PK on the target entity if a single id column is not desired
- Re-run the generator after updating the entity definitions
- Verify with entity.getPkProperty() != null in your schema setup code
Example fix
// before
Entity course = schema.addEntity("Course"); // no PK
// after
Entity course = schema.addEntity("Course");
course.addIdProperty().primaryKey(); Defensive patterns
Strategy: validation
Validate before calling
if (targetEntity.getPropertiesPk().isEmpty()) throw new IllegalStateException("Join-entity relation needs target PK"); Type guard
function hasPk(entity) { return entity != null && entity.getPkProperty() != null; } Try / catch
try { schema.generate(); } catch (RuntimeException e) { if (e.getMessage().startsWith("Target entity has no primary key")) { addPrimaryKey(targetEntity); } else { throw e; } } Prevention
- Always add an id PK to entities used as relation targets
- Check both source and target PKs when defining join-entity relations
- Lint the schema for PK-less entities before generation
When it happens
Trigger: Adding a many-to-many relation with a join entity where the target entity has no PK properties; the source entity passed its check but the target did not.
Common situations: Target table designed without a primary key (e.g. pure association payload table); forgetting addIdProperty() on the target entity; legacy schema import without PKs.
Understand the failure class
Background: "is required", "must be set", "missing required field": configuration validation errors across open-source libraries — this error's family across 36 libraries.
Related errors
- Source entity has no primary key, but we need it for
- Source entity has no primary key, but we need it for
- ( ) does not have a single-column primary key
- Unsupported type
- Type is already non-primitive
AI-assisted analysis of greenrobot/greenDAO@0bbb338e17 (2026-09-08).
Data as JSON: /api/errors/4b4191d4e87c9d61.
Report an issue: GitHub.
Appendix: source
Thrown at DaoGenerator/src/org/greenrobot/greendao/generator/ToManyWithJoinEntity.java:58
}
public Property getSourceProperty() {
return sourceProperty;
}
public Property getTargetProperty() {
return targetProperty;
}
void init3rdPass() {
super.init3rdPass();
List<Property> pks = sourceEntity.getPropertiesPk();
if (pks.isEmpty()) {
throw new RuntimeException("Source entity has no primary key, but we need it for " + this);
}
List<Property> pks2 = targetEntity.getPropertiesPk();
if (pks2.isEmpty()) {
throw new RuntimeException("Target entity has no primary key, but we need it for " + this);
}
}
}
View on GitHub (pinned to 0bbb338e17)