greenrobot/greenDAO · error · DaoException
Entity has no key
Error message
Entity has no key
What it means
getKeyVerified() found the entity non-null but getKey(entity) returned null, meaning the entity has no primary key assigned. greenDAO throws DaoException('Entity has no key') because callers of getKeyVerified require a guaranteed key.
Solutions
- insert(entity) first so the key is assigned before key-required operations.
- Use insertOrUpdate for upsert semantics.
- Check dao.getKey(entity) != null before proceeding.
- Assign the primary key manually if the schema permits (non-autoincrement).
Example fix
// before
if (dao.getKeyVerified(user) != null) { ... } // throws if not inserted
// after
K key = dao.getKey(user);
if (key == null) {
userDao.insert(user);
key = dao.getKey(user);
} Defensive patterns
Strategy: validation
Validate before calling
K key = dao.getKey(entity);
if (key == null) { dao.insert(entity); key = dao.getKey(entity); } Type guard
boolean hasKey(T e) { return e != null && dao.getKey(e) != null; } Try / catch
try {
K key = dao.getKeyVerified(entity);
} catch (DaoException e) {
if ("Entity has no key".equals(e.getMessage())) {
dao.insert(entity); // assign key, retry
} else throw e;
} Prevention
- Always insert before update/delete on fresh entities.
- Use insertOrUpdate for upsert semantics.
- Never strip the id field when copying/serializing entities.
- Check getKey() before key-required operations.
When it happens
Trigger: Calling getKeyVerified(entity) (directly or via update/delete internals) on a non-null entity whose @Id field is still null — i.e., never inserted.
Common situations: Updating or deleting freshly constructed/un-persisted entities; entities copied without id; entities deserialized from JSON/backups lacking the key field.
Understand the failure class
Background: "Not found" and "does not exist" errors: why "Task not found", "No such folder", and "Can't find" fire when a lookup comes back empty — this error's family across 14 libraries.
Related errors
- Cannot delete entity, key is null
- Cannot update entity without key - was it inserted before?
- Expected unique result, but count was
- Entity does not exist in the database anymore
- ( ) does not have a single-column primary key
AI-assisted analysis of greenrobot/greenDAO@0bbb338e17 (2026-09-08).
Data as JSON: /api/errors/ff4380786bd71a13.
Report an issue: GitHub.
Appendix: source
Thrown at DaoCore/src/main/java/org/greenrobot/greendao/AbstractDao.java:932
protected void assertSinglePk() {
if (config.pkColumns.length != 1) {
throw new DaoException(this + " (" + config.tablename + ") does not have a single-column primary key");
}
}
public long count() {
return statements.getCountStatement().simpleQueryForLong();
}
/** See {@link #getKey(Object)}, but guarantees that the returned key is never null (throws if null). */
protected K getKeyVerified(T entity) {
K key = getKey(entity);
if (key == null) {
if (entity == null) {
throw new NullPointerException("Entity may not be null");
} else {
throw new DaoException("Entity has no key");
}
} else {
return key;
}
}
/**
* The returned RxDao is a special DAO that let's you interact with Rx Observables without any Scheduler set
* for subscribeOn.
*
* @see #rx()
*/
@Experimental
public RxDao<T, K> rxPlain() {
if (rxDaoPlain == null) {
rxDaoPlain = new RxDao<>(this);
}
return rxDaoPlain;View on GitHub (pinned to 0bbb338e17)