插入特定列漂移颤振

Insert Specific columns Drift Flutter

我有一个 table,我想添加该 table 的一些列,但省略了 1 或 2 列,因为在添加时我没有这些特定列的数据。这是我的 table 和 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);
}

现在我没有 employee_complete_sync、user_company、employee_info_json 和 employee_pin 列的数据。但是当我尝试插入它们然后插入时抛出错误

employee_complete_sync:此值是必需的,但不存在

user_company:此值是必需的,但不存在

employee_info_json:此值是必需的,但不存在

employee_pin:此值是必需的,但不存在

我试图搜索如何自定义添加特定列,但没有找到任何解决方案。

如果我理解你某些列可以为空,如果是,那么漂移有 .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()();

此外,鉴于您的字段名称与命名构造函数 named('employee_pin') 相匹配,您可以避免它并直接执行

  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'))