gpt4 book ai didi

mysql - Clojure JDBC : MySQL errors when creating table with composite unique constraint

转载 作者:行者123 更新时间:2023-11-29 12:39:17 26 4
gpt4 key购买 nike

我尝试使用以下唯一约束创建下表。对于以下代码,我收到相同的 MySQL 错误,以及我的研究建议应该有效的三个类似变体。

(defn create-prices-table [db]
(db-do-commands db (create-table-ddl :prices
[:priceid :integer "PRIMARY KEY" "AUTO_INCREMENT"]
[:productid :integer "references products (productid)"]
[:date "date"]
[:price "decimal(7,2)"]
:table-spec "UNIQUE KEY `uc_price` (`productid`, `date`)")))

其他尝试...

;; :table-spec "UNIQUE `uc_price` (`productid`, `date`)"
;; :table-spec "constraint uc_price unique (productid, date)"
;; :table-spec "constraint `uc_price` unique (`productid`, `date`)"

我在尝试执行时收到的消息...

CompilerException java.sql.BatchUpdateException: 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 'UNIQUE KEY `uc_price` (`productid`, `date`)' at line 1, compiling:(schema_test.clj:38:41)

我正在使用 clojure.java.jdbc 库的 0.3.5 版本。

编辑

create-string-ddl 返回

"CREATE TABLE prices (priceid integer PRIMARY KEY AUTO_INCREMENT, productid integer references products (productid), date date, price decimal(7,2)) UNIQUE KEY `uc_price` (`productid`, `date`)"

最佳答案

我查看了 Clojure code这背后,似乎不支持约束子句。您可能可以通过执行以下操作来滥用 API:

(create-table-ddl :prices
[:priceid :integer "PRIMARY KEY" "AUTO_INCREMENT"]
[:productid :integer "references products (productid)"]
[:date "date"]
[:price "decimal(7,2), CONSTRAINT UNIQUE KEY `uc_price` (`productid`, `date`)"])

也就是说,您已经到了嵌入 MySQL 特定语法的阶段。您不妨自己编写创建表语句。

不幸的是,:table-spec 适用于 create table 中出现在括号之外的内容。请参阅table_option在 MySQL 文档中。

关于mysql - Clojure JDBC : MySQL errors when creating table with composite unique constraint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26346858/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com