【问题标题】:Clojure JDBC: MySQL errors when creating table with composite unique constraintClojure JDBC:创建具有复合唯一约束的表时出现 MySQL 错误
【发布时间】:2014-10-13 18:47:46
【问题描述】:

我尝试使用以下唯一约束创建下表。我收到以下代码的相同 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`)"

【问题讨论】:

  • 你从create-table-ddl函数得到什么字符串?

标签: mysql sql jdbc clojure


【解决方案1】:

我看过这背后的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 括号外的内容。请参阅 MySQL 文档中的table_option

【讨论】:

  • 谢谢,我试试看。
猜你喜欢
  • 1970-01-01
  • 2017-07-12
  • 2023-03-27
  • 2011-07-19
  • 2012-10-10
  • 2021-09-13
  • 2016-11-15
  • 1970-01-01
  • 2012-01-04
相关资源
最近更新 更多