You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Issue Description:
When i was using Slick's SourceCodeGenerator for Postgres. I found two issues.
When the db field type is timestamp, i've opened Joda Support, however when i finished generate code, i have to manually add code com.github.tototoshi.slick.PostgresJodaSupport._
When the db field type is _uuid, the final generated code is scala.collection.Seq, i think the generated code should be List[java.util.UUID]
It will be grateful if someone can help to explain or fix, i will glad too if you guys need me to provide more details
Test table schema
CREATETABLEpublic.order_comments (
id uuid NOT NULL,
vas_no varchar(16) NOT NULL,
comment_ts timestamp(3) NOT NULL,
"comment"varchar(1024) NULL,
file_ids _uuid NULL,
photo_ids _uuid NULL,
status_id varchar(32) NOT NULL,
comment_person varchar(64) NOT NULL,
comment_person_id uuid NOT NULL
);
Generate Script Code
CustomSourceCodeGenerator.scala
packagecom.code.genimportslick.codegen.SourceCodeGeneratorimportslick.model.ModelclassCustomSourceCodeGenerator(model: Model)
extendsSourceCodeGenerator(model) {
overridedefcode="import com.github.tototoshi.slick.PostgresJodaSupport._\n"+"import org.joda.time.DateTime\n"+super.code
overridedefTable=newTable(_) {
overridedefColumn=newColumn(_) {
// munge rawType -> SQL column type HERE (scaladoc in Slick 2.1.0 is outdated or incorrect, GeneratorHelpers#mapJdbcTypeString does not exist)// you can filter on model.name for the column name or model.tpe for the column type// your IDE won't like the String here but don't worry, the return type the compiler expects here is StringoverridedefrawType= model.tpe match {
// case "org.joda.time.LocalDateTime" => "DateTime" // kill j.s.Timestampcase _ => {
println(
s"${model.table.table}#${model.name} tpe=${model.tpe} rawType=${super.rawType}"
)
super.rawType
}
}
}
}
}
GenCode.scala
packagecom.code.genimportslick.jdbc.JdbcProfileimportcom.ben.MyPostgresProfile.api._importscala.concurrent.Awaitimportscala.concurrent.ExecutionContext.Implicits._importscala.concurrent.duration.DurationobjectGenCodeextendsApp {
valprofile="com.ben.MyPostgresProfile"// TODO: replace this with your Slick drivervaljdbcDriver="org.postgresql.Driver"// TODO: replace this with your JDBC drivervalurl="jdbc:postgresql://127.0.0.1:5433/test"// TODO: replace this with your database's JDBC URLvaloutputFolder="src/main/scala"// TODO: or whatever output folder you're in the mood forvalpkg="com.ben1"// TODO: your package namevaluser="postgres"// TODO: database username - optional, use forURL supports both with and without credentialsvalpassword=""// TODO: database password - optional, use forURL supports both with and without credentialsvaldriver:JdbcProfile=
com.ben.MyPostgresProfile// TODO: replace this with your Slick drivervaldb= {
// UNCOMMENT this if your database doesn't need credentials// driver.simple.Database.forURL(url, jdbcDriver)Database.forURL(
url,
driver = jdbcDriver,
user = user,
password = password
)
}
valmodelAction= com.ben.MyPostgresProfile
.createModel(
Some(com.ben.MyPostgresProfile.defaultTables)
)
.map(model =>newCustomSourceCodeGenerator(model).writeToMultipleFiles(
profile = profile,
folder = outputFolder,
pkg = pkg,
container ="Tables"
)
)
valrs= db.run(modelAction)
Await.result(rs, Duration.Inf)
}
PGProfile
MyPostgresProfile.scala
packagecom.benimportcom.github.tminglei.slickpg._importplay.api.libs.json.{JsValue, Json}
traitMyPostgresProfileextendsExPostgresProfilewithPgArraySupport// with PgDate2SupportwithPgDateSupportJodawithPgRangeSupportwithPgHStoreSupportwithPgPlayJsonSupportwithPgSearchSupport// with PgPostGISSupportwithPgNetSupportwithPgLTreeSupport {
defpgjson="jsonb"// jsonb support is in postgres 9.4.0 onward; for 9.3.x use "json"// Add back `capabilities.insertOrUpdate` to enable native `upsert` support; for postgres 9.5+overrideprotecteddefcomputeCapabilities:Set[slick.basic.Capability] =super.computeCapabilities + slick.jdbc.JdbcCapabilities.insertOrUpdate
overridevalapi=MyAPIobjectMyAPIextendsAPIwithArrayImplicitswithDateTimeImplicitswithJsonImplicitswithNetImplicitswithLTreeImplicitswithRangeImplicitswithHStoreImplicitswithSearchImplicitswithJodaDateTimePlainImplicitswithSearchAssistants {
implicitvalstrListTypeMapper=newSimpleArrayJdbcType[String]("text").to(_.toList)
implicitvalplayJsonArrayTypeMapper=newAdvancedArrayJdbcType[JsValue](
pgjson,
(s) =>
utils.SimpleArrayUtils.fromString[JsValue](Json.parse(_))(s).orNull,
(v) => utils.SimpleArrayUtils.mkString[JsValue](_.toString())(v)
).to(_.toList)
}
}
objectMyPostgresProfileextendsMyPostgresProfile
When i was using Slick's
SourceCodeGenerator
forPostgres
. I found two issues.timestamp
, i've opened Joda Support, however when i finished generate code, i have to manually add codecom.github.tototoshi.slick.PostgresJodaSupport._
_uuid
, the final generated code isscala.collection.Seq
, i think the generated code should beList[java.util.UUID]
It will be grateful if someone can help to explain or fix, i will glad too if you guys need me to provide more details
Generate Script Code
CustomSourceCodeGenerator.scala
GenCode.scala
PGProfile
MyPostgresProfile.scala
Generated Code:
OrderCommentsTable.scala
Sbt version information
buld.sbt
The text was updated successfully, but these errors were encountered: