Skip to content

Instantly share code, notes, and snippets.

@BlackPrincess
Last active August 29, 2015 13:55
Show Gist options
  • Select an option

  • Save BlackPrincess/8779513 to your computer and use it in GitHub Desktop.

Select an option

Save BlackPrincess/8779513 to your computer and use it in GitHub Desktop.
val posts = model.Post.joins(model.Post.tagsRef).findById(1) // thorws TooManyRowsException
insert into posts(
id,
title,
body,
created_at
) values (
1,
'test',
'text\body',
now()
);
insert into tags(
id,
name,
created_at
) select
1,
'tag1',
now()
union all select
2,
'tag2',
now();
insert into posts_tags(
id,
tag_id,
post_id,
created_at
)select
1,
1,
1,
now()
union all select
1,
2,
1,
now();
package model
import skinny.orm._, feature._
import scalikejdbc._, SQLInterpolation._
import org.joda.time._
case class Post(
id: Long,
title: String,
body: String,
tags: Seq[Tag] = Nil,
createdAt: DateTime,
updatedAt: Option[DateTime] = None
)
object Post extends SkinnyCRUDMapper[Post] with TimestampsFeature[Post] {
override val tableName = "posts"
override val defaultAlias = createAlias("p")
val tagsRef = hasManyThrough[Tag](
through = PostTag,
many = Tag,
merge = (p, t) => p.copy(tags = t)) // .byDefault
override def extract(rs: WrappedResultSet, rn: ResultName[Post]): Post = new Post(
id = rs.long(rn.id),
title = rs.string(rn.title),
body = rs.string(rn.body),
createdAt = rs.dateTime(rn.createdAt),
updatedAt = rs.dateTimeOpt(rn.updatedAt)
)
}
package model
import skinny.orm._, feature._
import scalikejdbc._, SQLInterpolation._
import org.joda.time._
case class PostTag(
id:Long,
tagId: Int,
postId: Int,
createdAt: DateTime
)
object PostTag extends SkinnyJoinTable[PostTag] {
override val tableName = "posts_tags"
override val defaultAlias = createAlias("pt")
override def extract(rs: WrappedResultSet, rn: ResultName[PostTag]): PostTag = new PostTag(
id = rs.long(rn.id),
tagId = rs.int(rn.tagId),
postId = rs.int(rn.postId),
createdAt = rs.dateTime(rn.createdAt)
)
}
package model
import skinny.orm._, feature._
import scalikejdbc._, SQLInterpolation._
import org.joda.time._
case class Tag(
id: Long,
name: String,
createdAt: DateTime,
updatedAt: Option[DateTime] = None
)
object Tag extends SkinnyCRUDMapper[Tag] with TimestampsFeature[Tag] {
override val tableName = "tags"
override val defaultAlias = createAlias("t")
override def extract(rs: WrappedResultSet, rn: ResultName[Tag]): Tag = new Tag(
id = rs.long(rn.id),
name = rs.string(rn.name),
createdAt = rs.dateTime(rn.createdAt),
updatedAt = rs.dateTimeOpt(rn.updatedAt)
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment