Moor: Dart Unhandled Exception: "SqliteException(1): no such column: valueName, SQL logic error (code 1)"

Created on 26 Feb 2021  路  4Comments  路  Source: simolus3/moor

Describe the bug

I created a fetch function that receives a custom expression content:

 Future<List<D>> fetch<T extends Table, D extends DataClass>(
      {TableInfo table, String whereExp}) async {
    final statement = select<T, D>(table);
    if (whereExp != null) {
      statement..where((Table u) {
        return CustomExpression(whereExp);
      });
    }
    return statement.get();
  }

if I pass whereExp as 'columnName = valueName'

then I get this exception:

Dart Unhandled Exception: "SqliteException(1): no such column: valueName, SQL logic error (code 1)"

What am I doing wrong here?

Thanks in advance!

Most helpful comment

Oh, I see! But passing whereExp: 'total_people = 100' should work then? If you want to pass the column and the target value as two different parameters, you could also use this:

Future<List<D>> fetch<T extends Table, D extends DataClass>({
    required TableInfo<T, D> table,
    String? column,
    Object? value,
  }) async {
    final statement = select<T, D>(table);
    if (column != null) {
      statement.where((T row) {
        final col = table.columnsByName[column]!;
        return col.equals(value);
      });
    }
    return statement.get();
  }

All 4 comments

Well, does the table have a column named valueName? Judging by your other issue it might just be called value_name?

Nono... when I say valueName I mean the value that I'm trying to compare the column to...

I'm basically trying to create a select statement like this:

    column_list
FROM
    table
WHERE
    columnName = valueName; // e.g total_people = 100

Oh, I see! But passing whereExp: 'total_people = 100' should work then? If you want to pass the column and the target value as two different parameters, you could also use this:

Future<List<D>> fetch<T extends Table, D extends DataClass>({
    required TableInfo<T, D> table,
    String? column,
    Object? value,
  }) async {
    final statement = select<T, D>(table);
    if (column != null) {
      statement.where((T row) {
        final col = table.columnsByName[column]!;
        return col.equals(value);
      });
    }
    return statement.get();
  }

Yes that works perfect! I made a change that works best for my use case:

Future<List<D>> fetch<T extends Table, D extends DataClass>({
    @required TableInfo<T, D> table,
    Map<String, dynamic> params
  }) async {
    final statement = select<T, D>(table);
    params.keys.forEach((key) {
      statement.where((T row) {
        final col = table.columnsByName[key];
        return col.equals(params[key]);
      });
    });
    return statement.get();
  }

But I was not aware of the columnsByName getter thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

simolus3 picture simolus3  路  4Comments

VadimOsovsky picture VadimOsovsky  路  3Comments

omidraha picture omidraha  路  3Comments

KKRoko picture KKRoko  路  3Comments

tony123S picture tony123S  路  4Comments