Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql Slick使用Postgres序列而不是序列_Postgresql_Scala_Slick - Fatal编程技术网

Postgresql Slick使用Postgres序列而不是序列

Postgresql Slick使用Postgres序列而不是序列,postgresql,scala,slick,Postgresql,Scala,Slick,我的postgres中有一个现有的模式,我想使用Slick,我如何强制这个id列在Slick中使用postgres序列 class User(id: Int, first: String, last: String) class Users(tag: Tag) extends Table[Int](tag, "users") { def id = column[Int]("id", O PrimaryKey) def * = id } val users = TableQuery[Us

我的postgres中有一个现有的模式,我想使用Slick,我如何强制这个id列在Slick中使用postgres序列

class User(id: Int, first: String, last: String)

class Users(tag: Tag) extends Table[Int](tag, "users") {
  def id = column[Int]("id", O PrimaryKey)
  def * = id
}
val users = TableQuery[Users]

val usersSequence = Sequence[Int]("users_seq") start 1 inc 1
这是我现有的模式:

create table users (
  id                 bigint not null,
  ...
)
create sequence users_seq;

正如@a_horse_和_no_name所说,一个序列列使用的是序列。在
Slick
中,您只需使用
AutoInc
定义列,在您的情况下:

def id = column[Int]("id", O.AutoInc, O.PrimaryKey)
PostgreSQL
侧定义
Serial
列时,只需返回一个
Int
列,默认值为:nextval('user\u id\u seq'::regclass),该序列用于增加值

无需在模式代码中手动创建序列:

class User(id: Int, first: String, last: String)

class Users(tag: Tag) extends Table[Int](tag, "users") {
  def id = column[Int]("id", O.AutoInc, O.PrimaryKey)
  def * = id
}

val users = TableQuery[Users]
评论后编辑:

我运行了sql语句,为序列添加了表alter:

create table users (
  id                 bigint not null
);

create sequence users_seq;
alter table users alter column id set default nextval('users_seq');
现在,在我的数据库中,我有一个表,其列
id
类型为
Int
,默认值为序列:
nextval('users\u seq'::regclass)
。这和我以前写的一样,你有一个表,它的序列应用于id列,光滑的代码也不会改变,你可以试着在本地数据库上运行它

请注意,您的sql语句是错误的,您确实创建了一个序列,但它没有分配给任何列,您必须使用alter或直接添加一个具有序列的列,如图所示。希望这更清楚


还有一件事我不能完全确定,
BigInt
Postgres字段是否映射到Scala
Long
,但可能与序列无关,但不确定,为了一致性,我总是将它们映射到
Long

一个
serial
列使用序列。所以我不确定你在问什么。我的意思是我想用Slick来阅读我当前现有的使用sequence和bigint的模式。我的意思是我想用Slick来阅读我当前现有的使用sequence和bigint的模式。我更新了问题。