greenrobot/greenDAO · error · RuntimeException

Source entity has no primary key, but we need it for

Error message

Source entity has no primary key, but we need it for 

What it means

ToManyWithJoinEntity.init3rdPass() requires the source entity of the many-to-many relation to have a primary key so the join entity can reference it. If sourceEntity.getPropertiesPk() is empty, the generator throws this RuntimeException naming the relation.

Solutions

  1. Add a primary key to the source entity (e.g. entity.addIdProperty().primaryKey())
  2. Or define a composite PK covering the columns you have via addXxxProperty().primaryKey() on multiple properties
  3. Re-run generation after fixing the entity definition
  4. Check both sides — the next check (target entity) has the same requirement

Example fix

// before
Entity student = schema.addEntity("Student"); // no PK
// after
Entity student = schema.addEntity("Student");
student.addIdProperty().primaryKey();
Defensive patterns

Strategy: validation

Validate before calling

if (sourceEntity.getPropertiesPk().isEmpty()) throw new IllegalStateException("Join-entity relation needs source PK");

Type guard

function hasPk(entity) { return entity != null && entity.getPkProperty() != null; }

Try / catch

try { schema.generate(); } catch (RuntimeException e) { if (e.getMessage().startsWith("Source entity has no primary key")) { addPrimaryKey(sourceEntity); } else { throw e; } }

Prevention

When it happens

Trigger: Adding a many-to-many relation with a join entity (ToManyWithJoinEntity) where the source entity defines no PK properties and generation reaches the 3rd pass.

Common situations: Join-table modeling where one side table was created without a primary key; forgetting addIdProperty() on the source entity; schema evolution that dropped a PK column.

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


AI-assisted analysis of greenrobot/greenDAO@0bbb338e17 (2026-09-08). Data as JSON: /api/errors/825b215e5ce84a29. Report an issue: GitHub.

Appendix: source

Thrown at DaoGenerator/src/org/greenrobot/greendao/generator/ToManyWithJoinEntity.java:54

    }

    public Entity getJoinEntity() {
        return joinEntity;
    }

    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)