(摩尔颤动)单个实体中的多个关系

时间:2020-10-03 00:06:20

标签: dart flutter-moor

我正在尝试建立两种关系:

  • 产品x颜色
  • 产品x尺寸

产品表:

class Products extends Table{
  IntColumn get idProduct => integer().autoIncrement()();
  //more fields...  
}

颜色表:

class Colors extends Table {
  IntColumn get idColor => integer().autoIncrement()();
  TextColumn get name => text().withLength(min: 1, max: 100)();
  TextColumn get value => text().withLength(min: 1, max: 100)();
}

尺寸表:

class Sizes extends Table {
  IntColumn get idSize => integer().autoIncrement()();
  TextColumn get size => text().withLength(min: 1, max: 100)();
}

产品可以具有多种尺寸和颜色。

我已经阅读过系泊文档,但仅找到具有一种关系的实体的示例。

2 个答案:

答案 0 :(得分:0)

使用查询解决。

首先,添加事务表:

    body {
      overflow-x: hidden;
      padding: 0;
      margin: 0;
      width: 100vw;
      height: 100vh;
      position: relative;

  }

  body::before {
      background-image: url("images/cab06e1517827eff989457c69aae7ea5.png");
      background-repeat: no-repeat;
      background-position: right;
      background-size: 600px;
      z-index: -1;
      position: absolute;
      width: 100%;
      height: 100%;
      top: -100%;
      left: -100%;
    }

然后,创建一个Dao进行查询

class ProductsWithColors extends Table{
  IntColumn get idProductWithColor => integer().autoIncrement()();
  IntColumn get product => integer()();
  IntColumn get color => integer()();
}

class ProductsWithSizes extends Table{
  IntColumn get idProductWithSize => integer().autoIncrement()();
  IntColumn get product => integer()();
  IntColumn get size => integer()();
}

这将生成以下方法:

@UseDao(tables: [Products, Colors, ProductsWithColors, ProductsWithSizes, Sizes],
 queries: {
  'productsWithSizesAndColors': 
      'SELECT * FROM products INNER JOIN products_with_colors ON
      products_with_colors.product = products.id_product INNER JOIN
      colors ON colors.id_color = products_with_colors.color 
      INNER JOIN products_with_sizes ON products_with_sizes.product = products.id_product 
      INNER JOIN sizes ON sizes.id_size = products_with_sizes.size
      WHERE <condition> = :idc'
  }
)

insert方法是这样的:

Future<List<ProductsWithSizesAndColorsResult>> productsWithSizesAndColors(int idc) {
  return productsWithSizesAndColorsQuery(idc).get();
}

Stream<List<ProductsWithSizesAndColorsResult>> watchProductosWithSizesAndColors(int idc) {
  return productsWithSizesAndColorsQuery(idc).watch();
}

答案 1 :(得分:0)

如果产品可以具有多种颜色和尺寸,并且每种颜色和尺寸可以具有多种颜色,则您需要两个表。一种是将颜色与产品链接在一起,另一种是将尺寸与产品链接在一起。

这是您仅提及其中一种关系的示例:https://moor.simonbinder.eu/docs/examples/relationships/

现在用两个来做。 (可以随意更改表名称以更合适)

表到带有颜色的产品:

class ProductsColors extends Table{
  IntColumn get idProductsColors => integer().autoIncrement()();
  IntColumn get Product => integer()();
  IntColumn get Color => integer()();
}

尺寸表的显示:

class ProductsSizes extends Table{
  IntColumn get idProductsColors => integer().autoIncrement()();
  IntColumn get Product => integer()();
  IntColumn get Size => integer()();
}

要为产品添加颜色或尺寸,只需创建具有正确ID的实体并将其添加到数据库中即可。

要选择特定组的颜色,请使用id“ idProductX”:

final SimpleSelectStatement<Colors, Color> colorQuery =
    select(colors)
      ..join(<Join<Table, DataClass>>[
        innerJoin(
          productsColors,
          productsColors.Color.equalsExp(colors.idColor) &
          productsColors.Product.equals(idProductX),
        ),
      ]);

List<Colors> = colorQuery.get();

“尺寸”应该等效。告诉我它是否对您有用。

为了提高安全性,您应该使int引用其他表外键,如下所示:

class ProductsColors extends Table{
  IntColumn get idProductsColors => integer().autoIncrement()();
  IntColumn get Product => integer()().customConstraint('REFERENCES products(idProduct)')();
  IntColumn get Color => integer()().customConstraint('REFERENCES colors(idColor)')();
}

但是您需要在数据库服务中添加此代码以激活外键:

@override
MigrationStrategy get migration =>
    MigrationStrategy(
      beforeOpen: (OpeningDetails details) async {
        await customStatement('PRAGMA foreign_keys = ON');
      },
    );