Squeryl和PostgreSQL的自动增量 [英] Squeryl and PostgreSQL's autoincrement
问题描述
当我尝试使用Squeryl的Table.insert在PostgreSQL表中插入新记录时,它会触发以下查询:
When I try to insert a new record in a PostgreSQL table using Squeryl's Table.insert, it fires this query:
insert into "users" ("id", "email", "fullname") values (nextval('"s_users_id"'),?,?)
这不起作用,因为我没有定义序列,而是将id列定义为自动增量:
This doesn't work, as I did not define a sequence and instead defined the id column to be "autoincrement":
CREATE TABLE Users (
id BIGSERIAL,
email varchar(255) NOT NULL,
fullname varchar(255),
PRIMARY KEY (id)
);
我在另一个论坛上阅读了有关此问题的旧帖子,我想知道现在的状态是什么。我们是否不鼓励使用自动增量并定义一个序列?还是有一个简单的解决方法来使它正常工作?
I read some old post in another forum about this issue, and I was wondering what the status is now. Are we discouraged to use autoincrement and define a sequence instead? Or is there a simple workaround to get this working?
编辑:事实上,刚才我看到自动增量本身会创建一个序列,但是使用另一个名称:users_id_seq可以告诉Squeryl使用此名称代替,还是遵循PostgreSQL的约定?
In fact just now I see that the autoincrement itself creates a serial, but under another name: users_id_seq Is there a way to tell Squeryl to look under this name instead, or follow PostgreSQL's convention?
推荐答案
您可以声明序列的名称:
You can declare name of the sequence:
case class Author(id: Long, firstName: String)
val author = table[Author]
on(author)(s => declare(
s.id is(autoIncremented("your_seq_name"))
))
更多信息: http:// squeryl。 org / schema-definition.html
这篇关于Squeryl和PostgreSQL的自动增量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!