greenrobot/greenDAO · error · DaoException
Table alias required
Error message
Table alias required
What it means
createSqlSelect builds a `SELECT ... FROM table alias` statement and prefixes each column with the table alias. A null (or empty-length) alias makes valid qualified column names impossible, so a DaoException is thrown. Note the check `length() < 0` is effectively `length() == 0`, so only null aliases realistically trigger it.
Solutions
- Always pass a non-null table alias (e.g. the entity's table name aliased as "T") to createSqlSelect
- Use QueryBuilder instead of calling SqlUtils directly so aliases are managed by the library
- Update/patch the call site that supplies the null alias
Example fix
// before
String sql = SqlUtils.createSqlSelect("USER", null, columns, false);
// after
String sql = SqlUtils.createSqlSelect("USER", "T", columns, false); Defensive patterns
Strategy: validation
Validate before calling
if (tableAlias == null || tableAlias.isEmpty()) throw new IllegalArgumentException("tableAlias required for createSqlSelect"); Try / catch
try {
sql = SqlUtils.createSqlSelect(table, alias, columns, distinct);
} catch (DaoException e) {
if (e.getMessage().equals("Table alias required")) {
sql = SqlUtils.createSqlSelect(table, table + "_T", columns, distinct);
}
} Prevention
- Prefer QueryBuilder over direct SqlUtils calls
- Always alias tables (e.g. "T") in custom SQL helpers
- Check aliases when copying join-related SQL code
When it happens
Trigger: Calling SqlUtils.createSqlSelect with tableAlias == null; internally this can happen when a query is built against a table without an alias where the code path requires one (e.g. joins or scoped queries passing a null alias).
Common situations: Custom query construction bypassing QueryBuilder; library-internal call paths after upgrades where an alias stopped being supplied; hand-written SQL helpers reused from join code.
Understand the failure class
Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.
Related errors
- Illegal parameter index:
- Limit must be set with QueryBuilder before it can be used…
- Offset must be set with QueryBuilder before it can be used…
- Expected unique result, but count was
- Duplicate property ordinals
AI-assisted analysis of greenrobot/greenDAO@0bbb338e17 (2026-09-08).
Data as JSON: /api/errors/87af84e14da65115.
Report an issue: GitHub.
Appendix: source
Thrown at DaoCore/src/main/java/org/greenrobot/greendao/internal/SqlUtils.java:109
}
}
return builder;
}
public static String createSqlInsert(String insertInto, String tablename, String[] columns) {
StringBuilder builder = new StringBuilder(insertInto);
builder.append('"').append(tablename).append('"').append(" (");
appendColumns(builder, columns);
builder.append(") VALUES (");
appendPlaceholders(builder, columns.length);
builder.append(')');
return builder.toString();
}
/** Creates an select for given columns with a trailing space */
public static String createSqlSelect(String tablename, String tableAlias, String[] columns, boolean distinct) {
if (tableAlias == null || tableAlias.length() < 0) {
throw new DaoException("Table alias required");
}
StringBuilder builder = new StringBuilder(distinct ? "SELECT DISTINCT " : "SELECT ");
SqlUtils.appendColumns(builder, tableAlias, columns).append(" FROM ");
builder.append('"').append(tablename).append('"').append(' ').append(tableAlias).append(' ');
return builder.toString();
}
/** Creates SELECT COUNT(*) with a trailing space. */
public static String createSqlSelectCountStar(String tablename, String tableAliasOrNull) {
StringBuilder builder = new StringBuilder("SELECT COUNT(*) FROM ");
builder.append('"').append(tablename).append('"').append(' ');
if (tableAliasOrNull != null) {
builder.append(tableAliasOrNull).append(' ');
}
return builder.toString();
}
View on GitHub (pinned to 0bbb338e17)