我正在尝试用SQLAlchemy重写这个mysql查询:
架构:
CREATE TABLE `posts` (
`post_id` INT UNSIGNED PRIMARY AUTO_INCREMENT,
`post_name` VARCHAR(255)
) Engine=InnoDB;
CREATE TABLE `post_tags` (
`tag_id` INT UNSIGNED PRIMARY AUTO_INCREMENT,
`tag_name` VARCHAR(255)
) Engine=InnoDB;
CREATE TABLE `post_tags_map` (
`map_id` INT PRIMARY AUTO_INCREMENT,
`post_id` INT NOT NULL,
`tags_id` INT NOT NULL,
FOREIGN KEY `post_id` REFERENCES `posts` (`post_id`),
FOREIGN KEY `post_id` REFERENCES `post_tags` (`tag_id`)
) Engine=InnoDB;
查询:
^{pr2}$以下是我所拥有的,但我一直得到:
1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SEPARATOR /))
rows = DBSession.query(Posts,func.group_concat(Post_Tags.tag_name.op('SEPARATOR')(literal_column('/')))).outerjoin(PostsTagsMap,Posts.post_id==PostsTagsMap.post_id).outerjoin(Post_Tags,PostsTagsMap.tags_id==Post_Tags.tag_id).group_by(Posts.post_id)
定义自定义函数元素似乎是处理
group_concat
方法的最简单方法。在像这样使用它:
^{pr2}$在测试上述示例和其他随机源代码时浪费了大量时间,以找出如何更改
func.group_concat()
中的标准分隔字符","
。。。然后你就可以找到一个符合你的直觉的论点了。 例如:所以,修改前面的答案:
^{pr2}$注意:这是python3.5、SQLite 3、Flask SQLAlchemy 2.3.2(安装了SQLAlchemy 1.0.13)
事实上,你做得对:
我不确定到底是怎么回事,因为你的模式似乎无效,你的查询拼错了表名,从错误判断,你试图把python代码输入mysql。在
对于那些像我一样,在google上找到这个页面的人来说,上面的代码应该很有帮助,还有源代码,显然:https://groups.google.com/forum/#!msg/sqlalchemy/eNbOoRJ425s/bScfQVat15EJ
相关问题 更多 >
编程相关推荐