tminglei / slick-pg

Slick extensions for PostgreSQL
BSD 2-Clause "Simplified" License
838 stars 180 forks source link

Strange type generated by codegen with slick-pg against table with jsonb columns #389

Closed henricook closed 6 years ago

henricook commented 6 years ago

Hi all,

I have a table, and i've got the jsonb columns being recognised successfully as JsValues by using slick-pg, and i'm at the point where i can insert a row successfully. Slick-codegen however generates a weird 'ResultsRow' definition which is stopping me reading that data back. See below for schema and examples:

I'm trying to do this:

db.run(Tables.Results.filter(report => report.reportid === reportId).result).map { matches => val firstMatch = matches.head ( firstMatch.commondata, firstMatch.beforealldata, firstMatch.step1, firstMatch.step2

The issue is that firstMatch is of type 'ResultsRow' but it has no accessible fields. I'm assuming it's because of:

Generates this scary looking row type:

/* Row type of table Results / type ResultsRow = HCons[String,HCons[String,HCons[String,HCons[java.sql.Timestamp,HCons[Option[play.api.libs.json.JsValue],HCons[Option[play.api.libs.json.JsValue],HCons[Option[play.api.libs.json.JsValue],HCons[Option[play.api.libs.json.JsValue],HCons[Option[play.api.libs.json.JsValue],HCons[Option[play.api.libs.json.JsValue],HCons[Option[play.api.libs.json.JsValue],HCons[Option[play.api.libs.json.JsValue],HCons[Option[play.api.libs.json.JsValue],HCons[Option[play.api.libs.json.JsValue],HCons[Option[play.api.libs.json.JsValue],HCons[Option[play.api.libs.json.JsValue],HCons[Option[Int],HCons[Option[Int],HCons[Option[Int],HCons[Option[Int],HCons[Option[play.api.libs.json.JsValue],HCons[Option[play.api.libs.json.JsValue],HCons[Option[play.api.libs.json.JsValue],HCons[Option[play.api.libs.json.JsValue],HNil]]]]]]]]]]]]]]]]]]]]]]]]

Other tables generate types like:

case class DownloadsandresourcesRow(id: java.util.UUID, resourcetype: String, linktitle: String, linkhref: String, filesizekilobytes: Option[String] = None, filetype: Option[String] = None)

Schema:

CREATE TABLE results ( reportId VARCHAR(255) NOT NULL, formType VARCHAR(10) NOT NULL, userEmail VARCHAR(255) NOT NULL, completedDateTime TIMESTAMP NOT NULL, commonData jsonb, beforeAllData jsonb, step1 jsonb, step2 jsonb, step3 jsonb, step4 jsonb, step5 jsonb, step6 jsonb, step7 jsonb, step8 jsonb, step9 jsonb, step10 jsonb, majors INTEGER, minors INTEGER, fails INTEGER, informationOnly INTEGER, majorQuestions jsonb, minorQuestions jsonb, failQuestions jsonb, informationOnlyQuestions jsonb, PRIMARY KEY (reportId) );

tminglei commented 6 years ago

@henricook well, you have more than 22 columns, which forced slick-codegen to use HList.

henricook commented 6 years ago

:-o I thought all the 22 limit type problems had gone with 2.12! Oh noes.

Do you know if it's still possible to access the data from the HList, or do i really need to just trim my table down?

tminglei commented 6 years ago

I'm not used to it. Maybe you can access the data like this: firstMatch(0) .. firstMatch(23).

henricook commented 6 years ago

Awesome thank you, that worked! You're a boss :+1: