Last active
January 21, 2017 16:51
-
-
Save j5ik2o/58a12bead21af58fa344b9c64dfedc69 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE `users` ( | |
`id` BIGINT NOT NULL AUTO_INCREMENT, | |
`name` VARCHAR(64) NOT NULL, | |
`email` VARCHAR(254) NOT NULL, | |
`password` VARCHAR(64) NOT NULL, | |
`create_at` TIMESTAMP NOT NULL DEFAULT current_timestamp, | |
`update_at` TIMESTAMP NOT NULL DEFAULT current_timestamp, | |
PRIMARY KEY (`id`), | |
UNIQUE INDEX `email_UNIQUE` (`email` ASC) | |
) ENGINE=InnoDB; | |
CREATE TABLE `user_follows` ( | |
`id` BIGINT NOT NULL AUTO_INCREMENT, | |
`user_id` BIGINT NOT NULL, | |
`follow_id` BIGINT NOT NULL, | |
`create_at` TIMESTAMP NOT NULL DEFAULT current_timestamp, | |
`update_at` TIMESTAMP NOT NULL DEFAULT current_timestamp, | |
PRIMARY KEY (`id`), | |
INDEX (`user_id`), | |
FOREIGN KEY (`user_id`) REFERENCES `users` (`id`), | |
FOREIGN KEY (`follow_id`) REFERENCES `users` (`id`) | |
) ENGINE=InnoDB; | |
-- salt = $2a$10$pNy2jKGCbHTYB0cCy4vSWu, test? users have password 'test'. | |
INSERT INTO microposts.users (name, email, password, create_at, update_at) VALUES ('test1', '[email protected]', '$2a$10$pNy2jKGCbHTYB0cCy4vSWumSwKeqeQZSr/qyc6HGF3hDMUJkyPgGi', now(), now()); | |
INSERT INTO microposts.users (name, email, password, create_at, update_at) VALUES ('test2', '[email protected]', '$2a$10$pNy2jKGCbHTYB0cCy4vSWumSwKeqeQZSr/qyc6HGF3hDMUJkyPgGi', now(), now()); | |
INSERT INTO microposts.users (name, email, password, create_at, update_at) VALUES ('test3', '[email protected]', '$2a$10$pNy2jKGCbHTYB0cCy4vSWumSwKeqeQZSr/qyc6HGF3hDMUJkyPgGi', now(), now()); | |
INSERT INTO microposts.users (name, email, password, create_at, update_at) VALUES ('test4', '[email protected]', '$2a$10$pNy2jKGCbHTYB0cCy4vSWumSwKeqeQZSr/qyc6HGF3hDMUJkyPgGi', now(), now()); | |
INSERT INTO microposts.users (name, email, password, create_at, update_at) VALUES ('test5', '[email protected]', '$2a$10$pNy2jKGCbHTYB0cCy4vSWumSwKeqeQZSr/qyc6HGF3hDMUJkyPgGi', now(), now()); | |
INSERT INTO microposts.users (name, email, password, create_at, update_at) VALUES ('test6', '[email protected]', '$2a$10$pNy2jKGCbHTYB0cCy4vSWumSwKeqeQZSr/qyc6HGF3hDMUJkyPgGi', now(), now()); | |
INSERT INTO microposts.user_follows (user_id, follow_id, create_at, update_at) VALUES (1, 2, now(), now()); | |
INSERT INTO microposts.user_follows (user_id, follow_id, create_at, update_at) VALUES (1, 3, now(), now()); | |
INSERT INTO microposts.user_follows (user_id, follow_id, create_at, update_at) VALUES (1, 4, now(), now()); | |
INSERT INTO microposts.user_follows (user_id, follow_id, create_at, update_at) VALUES (1, 5, now(), now()); | |
INSERT INTO microposts.user_follows (user_id, follow_id, create_at, update_at) VALUES (1, 6, now(), now()); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package models | |
import org.joda.time.DateTime | |
import scalikejdbc.WrappedResultSet | |
import skinny.orm.{ Alias, SkinnyCRUDMapper } | |
case class User( | |
id: Option[Long] = None, | |
name: String, | |
email: String, | |
password: String, | |
createAt: DateTime = DateTime.now(), | |
updateAt: DateTime = DateTime.now() | |
) | |
object User extends SkinnyCRUDMapper[User] { | |
override def schemaName: Option[String] = Some("microposts") | |
override def tableName = "users" | |
override def defaultAlias: Alias[User] = createAlias("u") | |
def toNamedValues(record: User): Seq[(Symbol, Any)] = Seq( | |
'name -> record.name, | |
'email -> record.email, | |
'password -> record.password, | |
'createAt -> record.createAt, | |
'updateAt -> record.updateAt | |
) | |
override def extract(rs: WrappedResultSet, n: scalikejdbc.ResultName[User]): User = | |
new User( | |
rs.get(n.id), | |
rs.get(n.name), | |
rs.get(n.email), | |
rs.get(n.password), | |
rs.get(n.createAt), | |
rs.get(n.updateAt) | |
) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package models | |
import org.joda.time.DateTime | |
import scalikejdbc.WrappedResultSet | |
import skinny.orm.{ Alias, SkinnyCRUDMapper } | |
case class UserFollow( | |
id: Option[Long], | |
userId: Long, | |
followId: Long, | |
createAt: DateTime = DateTime.now(), | |
updateAt: DateTime = DateTime.now(), | |
user: Option[User] = None, // mapping it with userId | |
followUser: Option[User] = None // mapping it with followId | |
) | |
object UserFollow extends SkinnyCRUDMapper[UserFollow] { | |
override def schemaName: Option[String] = Some("microposts") | |
override def tableName = "user_follows" | |
override def defaultAlias: Alias[UserFollow] = createAlias("uf") | |
def toNamedValues(record: UserFollow): Seq[(Symbol, Any)] = Seq( | |
'userId -> record.userId, | |
'followId -> record.followId, | |
'createAt -> record.createAt, | |
'updateAt -> record.updateAt | |
) | |
override def extract(rs: WrappedResultSet, n: scalikejdbc.ResultName[UserFollow]): UserFollow = | |
new UserFollow( | |
rs.get(n.id), | |
rs.get(n.userId), | |
rs.get(n.followId), | |
rs.get(n.createAt), | |
rs.get(n.updateAt) | |
) | |
} |
seratch
commented
Jan 21, 2017
最終的に以下でやりたいことができました。ありがとうございました。
lazy val u1 = User.createAlias("u1")
lazy val userRef = belongsToWithAliasAndFkAndJoinCondition[User](
right = User -> u1,
fk = "userId",
on = sqls.eq(defaultAlias.userId, u1.id),
merge = (uf, f) => uf.copy(user = f)
)
lazy val u2 = User.createAlias("u2")
lazy val followRef = belongsToWithAliasAndFkAndJoinCondition[User](
right = User -> u2,
fk = "followId",
on = sqls.eq(defaultAlias.followId, u2.id),
merge = (uf, f) => uf.copy(followUser = f)
)
lazy val allAssociations: CRUDFeatureWithId[Long, UserFollow] = joins(userRef, followRef)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment