Вопрос

I'm learning Haskell, and my first real app is a web application using Yesod + Database.Persist with the SQLite backend, but I can't find a way to specify some things with the persistLowerCase quasi quoter:

  • How to declare a specified column should be indexed (without the uniqueness constraint)
  • How to specify foreign keys (I can only do foreign_table_id ForeignTableId, which translates to "foreign_table_id" INTEGER NOT NULL REFERENCES "foreign_table")
  • How to specify column sizes (VARCHAR(50), for instance)
  • How to use CHAR and BLOB as a column type

The last two are not useful for SQLite proper, but I might change database backends in the future.

Это было полезно?

Решение

If I remember correctly (from a presentation about Yesod at InfoQ), adding an index must be done manually, there's no support in Yesod for doing it automatically.

Column sizes can be specified by typing maxlen=<size> in the field definition. BLOBs can be stored as a ByteStrings. An example for both fields looks like this:

share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
    Something
        b           ByteString
        t           Text         maxlen=30
|]

The BLOB data type will work with Sqlite3, since I've used it in the past, but I don't know for sure about the maxlen attribute, I just read about it. You can find more details in the Yesod book, chapter about Persistent.

As for the foreign keys, my experience tells me that those are synonyms. Just don't forget to enable foreign keys, since they're disabled by default.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top