0

我有一个这样定义的列:

@Column(nullable: true)
Document openHours; // List "openHours": ["Tuesday - Sunday: 11.00 - 21.00"],

在我的迁移文件中,我使用种子():

@override
Future seed() async {
const sClientSQL = ...

让我失望的 json 部分是:

"openHours": [
    "Monday - Friday: 17.00 - 22.00",
    "Saturday: 14:00 - 23:00",
    "Sunday & Holidays: 11:30 - 22:00"
  ],

终端中的错误如下所示:

Seeding data from migration version 1...
*** There was an issue. Reason: Could not infer type of value '[Monday - Friday: 17.00 - 22.00,    
Saturday: 14:00 - 23:00, Sunday & Holidays: 11:30 - 22:00]'.. Table: null Column: null

文档说:

Document    map or list (Map<String, dynamic> or List<dynamic>)

JSON 对我来说看起来不错,但显然在这种情况下它是错误的。那么我在这里做错了什么?我找不到有关如何在渡槽中为 Document 类型编写 json 部分的示例。

谢谢大家并问候安东尼奥

[编辑 4]

下面是一个简短的查询示例:

const sClientSQL = 'INSERT INTO _Clients (name, owner, address, contacts, openhours, dayOff, description) VALUES (@name, @owner, @address, @contacts, @openhours, @dayOff, @description)';

await database.store.execute(sClientSQL, substitutionValues: {
  'name': 'Dolce Vita',
  'owner': 'David Driss',
  'address': {
    'street': 'Johannisstr. 3',
    'zipCode': '54290',
    'city': 'Trier'
  },
  'contacts': {
    'phone': '0651 94 90 40',
    'fax': '0651 43 23 2',
    'mail': 'info@dolcevita.de'
  },   
  'openhours': [
    "Montag - Freitag: 17.00 - 22.00",
    "Samstag: 14:00 - 23:00",
    "Sonntag & Feiertag: 11:30 - 22:00"
  ],
  'dayOff': '',
  'description': 'Alle Speisen und Getränke sind inkl. MwST. Extrazutaten werden gesondert berechnet'
});

[编辑 1]

更多信息:

替换值与 Map 一样。

我使用了双引号,并将其更改为单引号,但没有效果。

[编辑 2]

在 dartpad 中尝试了复杂的地图并创建了一个要点来显示:

Dart Maps 示例的要点将代码放入 dartpad。

结果原样的地图是有效的。

[编辑 3]

  1. 我删除了所有 json 列以确保其正常工作。成功。
  2. 添加了一个 json 列,这是我之前展示的第一个示例。同样的问题
  3. 试图手动插入 jsonb 列。成功。

所以,只有await database.store.execute命令不想要我的 json 类型文字。

4

1 回答 1

0

在另一个频道的用户的帮助下,我终于想出了如何让它工作。

神奇之处在于使用正确的语法,并且经过了很多试验和错误,想知道在哪里可以找到有关它的文档?

  • 使用多行时,在开头和结尾使用三个单引号 '''
  • 在多行表达式中,对 json 使用双引号

工作示例

替换值中的所有以下“字段”都是数据库中的 jsonb 列

  • 地址联系人是 json 对象的示例
  • openhours是一个 json 字符串数组的示例,多行
  • fess是一个 json 对象数组的示例,多行
await database.store.execute(sClientSQL, substitutionValues: {
  'address': {
    'street': 'a street',
    'zipCode': '123456',
    'city': 'mycity'
  },
  'contacts': {
    'phone': '12345678',
    'fax': '12346578',
    'mail': 'info@mydomain.com'
  },         
  'openhours': '''[
    "Monday - Friday: 17.00 - 22.00",
    "Saturday: 14:00 - 23:00",
    "Sunday: 11:30 - 22:00"
  ]''', 
  'fees': '''[
    {"delivery": 2.00, "minOrder": 7.50, "location": "all"},
    {"delivery": 2.50, "minOrder": 9.50, "location": "here"},
    {"delivery": 2.50, "minOrder": 9.50, "location": "there"}
  ]''',

我认为通过这些我们可以完成所有复杂性。

于 2020-06-17T17:29:21.587 回答