我正在尝试使用Common创建一个简单的数据库。我使用PostgreSQL和CLSQL。我可以创建类和生成表,但是当我想要插入一个没有主键的值以获得生成的值时,它就不能工作。它似乎适用于mysql数据库。用PostgreSQL可以做到这一点吗?
我将主键定义为:
(id :db-kind :key
:db-type "serial"
:db-constraints (:not-null :unique)
:type integer
:initarg :id)我得到了一个错误:
While accessing database #<POSTGRESQL-DATABASE localhost/cl_ormex/postgres OPEN {1004FCC403}>
with expression "SELECT currval ('NIL')":
Error 42P01 / relation "nil" does not exist
LINE 1: SELECT currval ('NIL')
^
has occurred.
[Condition of type SQL-DATABASE-DATA-ERROR]我在SBCL1.3.1中使用了PostgreSQL 9.5.2。
编辑
下面是一个例子:
(require 'clsql)
(defpackage :orm-ex (:use :cl :clsql))
(in-package :orm-ex)
(file-enable-sql-reader-syntax)
(enable-sql-reader-syntax)
(setf *default-caching* nil)
(connect '("localhost" "examp" "postgres" "postgres")
:database-type :postgresql)
(def-view-class person ()
((id :db-kind :key
:db-type "serial"
:db-constraints (:not-null :unique)
:type integer
:initarg :id
:accessor person-id)
(name :type (varchar 30)
:initarg :name
:accessor person-name)))
(defparameter person1
(make-instance 'person
:name "Matt"))
(dolist (c '(person)) (create-view-from-class c))
(update-records-from-instance person1)我并不真正理解这个错误,但是该行似乎是插入到数据库中的。
发布于 2016-06-08 14:49:28
看来这是行不通的。它有一个todo文件,上面写着:
所以也许它不适用于Postgres。我相信你有很多可能性。
使用更多更新的其他框架,如cl、请看这里:
cl为各种特定于数据库服务器的库(、cl等)提供了统一的接口。SxQL为构建安全、自动参数化的SQL查询提供了DSL。 有两个相当完整的ORMs:克莱恩,由你的真实,和积分,由作者的cl。整合:不鼓励使用cl以外的任何东西。未来工作:存在其他数据库系统(如Oracle )的绑定。为cl编写驱动程序将是最佳的行动方针,有助于整合。
我发现很容易生成in,这些in可以按创建时间按升序或降序排序,也可以使用生成器或考虑插入的最后一个数字。
但是,这会使通用时间和增加一个随机数来同时创建多个实体,并且碰撞率很低。
(format nil "~12,'0d-~6,'0d" (get-universal-time) (random 1000000))发布于 2019-10-25 06:58:25
从Mark的爱Lisp开始,db-constraints需要用:auto-increment来定义。
Note --截至今天的图书版本(25/10/2019)不正确,但下载的代码如下:
(clsql:def-view-class article ()
((id
:db-kind :key
:db-constraints (:auto-increment :not-null :unique)
:type integer
:initarg :id)
(uri
:accessor uri
:type (string 60)
:initarg :uri)
(title
:accessor title
:type (string 90)
:initarg :title)
(text
:accessor text
:type (string 500)
:nulls-ok t
:initarg :text)))https://stackoverflow.com/questions/36687705
复制相似问题