yesod持久postgresql复杂记录

Maa*_*ten 16 database orm haskell persistent yesod

我使用persistent来保存以下记录(时间是UTCTime).

type Price = Int
type Volume = Int

share [mkPersist sqlSettings, mkMigrate "migrateBook"] 
  [persistLowerCase|
    Level
      limit     Price
      volumes   [Volume]
      deriving Show Read Eq

    Book
      time      Time
      asks      [Level]
      bids      [Level]
      deriving Show Read Eq
  |]
Run Code Online (Sandbox Code Playgroud)

当我迁移结构时,我得到一个Book表:

CREATE TABLE book
(
 id serial NOT NULL,
 "time" timestamp without time zone NOT NULL,
 asks character varying NOT NULL,
 bids character varying NOT NULL,
 CONSTRAINT book_pkey PRIMARY KEY (id )
 )
Run Code Online (Sandbox Code Playgroud)

和一个表级别:

 CREATE TABLE level
 (
   id serial NOT NULL,
   "limit" double precision NOT NULL,
   volumes character varying NOT NULL,
   CONSTRAINT level_pkey PRIMARY KEY (id )
 )
Run Code Online (Sandbox Code Playgroud)

插入书籍时,级别表保持为空,Book表包含一个包含预期记录的JSON版本的条目.

问题:

如何使用列的实际简单类型(例如int和time)而不是复杂类型的JSON?

Persistent是否知道如何存储多对多的关系?

例如,给定一个带有list :: [B]的记录A,我可以用它来创建第三个表

AId | B
-------
 1  | b1
 1  | b2 etc
Run Code Online (Sandbox Code Playgroud)

我使用以下包:

persistent-postgresql-1.0.3
yesod-persistent-1.1.0.1
Postgres 9.1
Run Code Online (Sandbox Code Playgroud)

Tho*_*mas 1

首先,对于[Level],仅存储 ID 允许您在 json 列表中存储简单类型。

type Price = Int
type Volume = Int

share [mkPersist sqlSettings, mkMigrate "migrateBook"] 
  [persistLowerCase|
    Level
      limit     Price
      volumes   [Volume]
      deriving Show Read Eq

    Book
      time      Time
      asks      [LevelId]
      bids      [LevelId]
      deriving Show Read Eq
  |]
Run Code Online (Sandbox Code Playgroud)

或者,如果需要跨关系查询,则需要定义“直通”表或 M2M 表,就像正常的关系数据库设计一样。

type Price = Int
type Volume = Int

share [mkPersist sqlSettings, mkMigrate "migrateBook"] 
  [persistLowerCase|
    Level
      limit     Price
      volumes   [Volume]
      deriving Show Read Eq

    Book
      time      Time
      deriving Show Read Eq

    BookAsk
      book      BookId
      level     LevelId

    BookBid
      book      BookId
      level     LevelId
  |]
Run Code Online (Sandbox Code Playgroud)

与 for 的情况一样[Volume],问题出在 permanent-postgresql 中。persist 硬连线将PersistList值编组为 JSON,而不是使用 postgres 对数组列类型的本机支持。如果您想解决此问题,则需要提交问题或拉取请求。