Home > OS >  Insert Specific columns Drift Flutter
Insert Specific columns Drift Flutter

Time:04-19

I have a table and I want to add some columns of that table but omit 1 or 2 columns as while adding I do not have data for these specific columns. this is my table and dao

class LoginEmployee extends Table {

  TextColumn get employee_id => text().named("employee_id")();

  TextColumn get employee_store => text().named("employee_store")();

  TextColumn get user_company => text().named("user_company")();

  TextColumn get employee_email => text().named("employee_email")();

  TextColumn get employee_password => text().named("employee_password")();

  TextColumn get employee_pin => text().named("employee_pin")();

  TextColumn get employee_info_json => text().named("employee_info_json")();

  TextColumn get employee_complete_sync =>
      text().named("employee_complete_sync")();

  // Making name as the primary key of a tag requires names to be unique
  @override
  Set<Column> get primaryKey => {employee_id, employee_store};
}

**DAO class:**

@DriftAccessor(tables: [LoginEmployee])
class LoginEmployeeDao extends DatabaseAccessor<AppDb>
    with _$LoginEmployeeDaoMixin {
  LoginEmployeeDao(AppDb db) : super(db);

  Future<List<LoginEmployeeData>> getCategories() async {
    return await select(loginEmployee).get();
  }

  Future<LoginEmployeeData> getLoginEmployee(String id, storeId) async {
    return await (select(loginEmployee)
          ..where((tbl) =>
              tbl.employee_id.equals(id) & tbl.employee_store.equals(storeId)))
        .getSingle();
  }

  Future<int> insertLoginEmployee(LoginEmployeeCompanion entity) async {
    return await into(loginEmployee).insert(entity);
  }

  Future<bool> updateLoginEmployee(LoginEmployeeCompanion entity) async {
    return await update(loginEmployee).replace(entity);
  }

  Future<int> deleteLoginEmployee(String id, storeId) async {
    return await (delete(loginEmployee)
          ..where((tbl) =>
              tbl.employee_id.equals(id) & tbl.employee_store.equals(storeId)))
        .go();
  }
}

**Now this is how I am adding employee:**

  _loginEmployeeDao
            .insertLoginEmployee(DataMapper.toEmployee(emp))
            .then((value) {
          debugPrint('resultOnSaving-x ${value}');
        });

DataMapper class:

class DataMapper {
  static LoginEmployeeCompanion toEmployee(User? user,{String pass=''}) =>
      const LoginEmployeeCompanion().copyWith(
          employee_id: Value(user?.id.toString() ?? ''),
          employee_email: Value(user?.email ?? ''),
          employee_pin: null,
          employee_store: Value(user?.storeId.toString() ?? ''),
          employee_info_json: null,
          employee_password: Value(pass),
          employee_complete_sync:  null,
          user_company: null);
}

Now I don't have data for columns employee_complete_sync,user_company,employee_info_json and employee_pin. but when I try to insert them then insert throw errors

employee_complete_sync: This value was required, but isn't present

user_company: This value was required, but isn't present

employee_info_json : This value was required, but isn't present

employee_pin: This value was required, but isn't present

I tried to search for that how to custom add specific columns but did not found any solution for this.

CodePudding user response:

If I understood you some columns can be null right, If yes then drift has .nullable()()

Marks this column as nullable. Nullable columns should not appear in a primary key. Columns are non-null by default.

  TextColumn? get employee_pin => text().named('employee_pin').nullable()();

Also given that your field name matches the named constructor named('employee_pin'), you can avoid it and just do

  TextColumn? get employee_pin => text().nullable()();

By default, the field name will be used as the column name, e.g. IntColumn get id = integer() will have "id" as its associated name. Columns made up of multiple words are expected to be in camelCase and will be converted to snake_case (e.g. a getter called accountCreationDate will result in an SQL column called account_creation_date). To change this default behavior, use something like IntColumn get id = integer((c) => c.named('user_id'))

  • Related