{"record":{"id":"87af84e14da65115","repo":"greenrobot/greenDAO","slug":"table-alias-required","errorCode":null,"errorMessage":"Table alias required","messagePattern":"Table alias required","errorType":"exception","errorClass":"DaoException","httpStatus":null,"severity":"error","filePath":"DaoCore/src/main/java/org/greenrobot/greendao/internal/SqlUtils.java","lineNumber":109,"sourceCode":"            }\n        }\n        return builder;\n    }\n\n    public static String createSqlInsert(String insertInto, String tablename, String[] columns) {\n        StringBuilder builder = new StringBuilder(insertInto);\n        builder.append('\"').append(tablename).append('\"').append(\" (\");\n        appendColumns(builder, columns);\n        builder.append(\") VALUES (\");\n        appendPlaceholders(builder, columns.length);\n        builder.append(')');\n        return builder.toString();\n    }\n\n    /** Creates an select for given columns with a trailing space */\n    public static String createSqlSelect(String tablename, String tableAlias, String[] columns, boolean distinct) {\n        if (tableAlias == null || tableAlias.length() < 0) {\n            throw new DaoException(\"Table alias required\");\n        }\n\n        StringBuilder builder = new StringBuilder(distinct ? \"SELECT DISTINCT \" : \"SELECT \");\n        SqlUtils.appendColumns(builder, tableAlias, columns).append(\" FROM \");\n        builder.append('\"').append(tablename).append('\"').append(' ').append(tableAlias).append(' ');\n        return builder.toString();\n    }\n\n    /** Creates SELECT COUNT(*) with a trailing space. */\n    public static String createSqlSelectCountStar(String tablename, String tableAliasOrNull) {\n        StringBuilder builder = new StringBuilder(\"SELECT COUNT(*) FROM \");\n        builder.append('\"').append(tablename).append('\"').append(' ');\n        if (tableAliasOrNull != null) {\n            builder.append(tableAliasOrNull).append(' ');\n        }\n        return builder.toString();\n    }\n","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/greenrobot/greenDAO/blob/0bbb338e17c4cf28aba5aae62d42f73f50186157/DaoCore/src/main/java/org/greenrobot/greendao/internal/SqlUtils.java#L91-L127","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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"],"exampleFix":"// before\nString sql = SqlUtils.createSqlSelect(\"USER\", null, columns, false);\n// after\nString sql = SqlUtils.createSqlSelect(\"USER\", \"T\", columns, false);","handlingStrategy":"validation","validationCode":"if (tableAlias == null || tableAlias.isEmpty()) throw new IllegalArgumentException(\"tableAlias required for createSqlSelect\");","typeGuard":null,"tryCatchPattern":"try {\n    sql = SqlUtils.createSqlSelect(table, alias, columns, distinct);\n} catch (DaoException e) {\n    if (e.getMessage().equals(\"Table alias required\")) {\n        sql = SqlUtils.createSqlSelect(table, table + \"_T\", columns, distinct);\n    }\n}","preventionTips":["Prefer QueryBuilder over direct SqlUtils calls","Always alias tables (e.g. \"T\") in custom SQL helpers","Check aliases when copying join-related SQL code"],"tags":["greendao","orm","sql","query"],"backgroundTag":"missing-required-argument","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"}