primary key column count is not allowed to reference an aggregate expression

尝试在 Vitess 里面创建 Materialize View,命令如下:

vtctlclient Materialize '{"workflow": "aa1", "source_keyspace": "commerce", "target_keyspace": "commerce", "table_settings": [{"target_table": "product_count", "source_expression": "select count(*) AS count, price from product2 group by price", "create_ddl": "copy"}]}

结果 product_count 表中并没有数据,打开 master tablet 的状态页面,对应 workflow 提示错误:

primary key column count is not allowed to reference an aggregate expression

查看官方文档:

The select expressions can be any non-aggregate MySQL expression, or they can also be count or sum as aggregate expressions. Aggregate expressions combined with the corresponding group by clauses will allow you to materialize real-time rollups of the source table, which can be used for analytics. 

应该是可以支持 countsum 的,于是去 slack 频道寻求帮助,得到回复:

Aggregate expressions are allowed and yours looks good. One issue I see is that create_ddl is set as copy. Is there a product2 in the source? The error you see is due to the fact that vreplication finds the count column as the primary key of the target table.
Can you try specifying "create_ddl", say as, "create table product_count(count int, price int, primary key (price))"

发现本地创建的 product_count 表缺少 primary key,加上后便一切正常,在随后的交流中,PlanetScale 的 CTO sougou 同学回复:

if there's no primary key, we treat all columns as pk. But it's a hack. It handles some common use cases, but will break, like if there are null values, etc.
We could add an additional check to print a better error message though

PlanetScale 团队在特别积极地维护 Vitess 社区,有问必答 👍🙏

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值