Scala [2.0.1] model and mysql datetime type

478 views
Skip to first unread message

danny

unread,
Jun 6, 2012, 11:08:12 AM6/6/12
to play-fr...@googlegroups.com
Hi

I'm so new to this :( so sorry for the silly questions :(

I have this Table structure in MYSQL :


CREATE TABLE IF NOT EXISTS `phone_apps` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL,
  `description` text,
  `price` float DEFAULT NULL,
  `created` datetime DEFAULT NULL,
  `modified` datetime DEFAULT NULL,
  `url` varchar(255) DEFAULT NULL,
  `pic1` varchar(255) DEFAULT NULL,
  `pic2` varchar(255) DEFAULT NULL,
  `pic3` varchar(255) DEFAULT NULL,
  `pic4` varchar(255) DEFAULT NULL,
  `pic5` varchar(255) DEFAULT NULL,
  `category_id` int(11) NOT NULL,
  `platform_id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;


 how does the mysql datetime type and PRIMARY KEY work in the Scala Play Model :


 id: Long,
  name: String,
  description: String,
  url: String,

  .....


Thanks
Danny





 

johanandren

unread,
Jun 6, 2012, 11:14:46 AM6/6/12
to play-fr...@googlegroups.com
Not sure if this is your question but:
the id column could map to a Pk[Int] or a Pk[Long] 
the DATETIME to a Option[java.util.Date] (Date since a java date is actually more of a datetime, Option - since it could be NULL/missing from the database, if it was NOT NULL it would be a java Date)

danny

unread,
Jun 6, 2012, 11:46:06 AM6/6/12
to play-fr...@googlegroups.com
thanks that is the question :)

Do you need a Model when using Anorm?

Thanks
Danny

danny

unread,
Jun 6, 2012, 11:54:30 AM6/6/12
to play-fr...@googlegroups.com
Hi

I've changed the SQL a bit 'DateTime' can now be null!



CREATE TABLE IF NOT EXISTS `phone_apps` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL,
  `description` text,
  `price` float DEFAULT NULL,
  `created` datetime DEFAULT NULL,
  `modified` datetime DEFAULT NULL,
  `url` varchar(255) DEFAULT NULL,
  `pic1` varchar(255) DEFAULT NULL,
  `pic2` varchar(255) DEFAULT NULL,
  `pic3` varchar(255) DEFAULT NULL,
  `pic4` varchar(255) DEFAULT NULL,
  `pic5` varchar(255) DEFAULT NULL,
  `category_id` int(11) DEFAULT NULL,
  `platform_id` int(11) DEFAULT NULL,

  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

and the Scala Model:

case class Apps {

  id: Pk[Long]

  name: String,
  description: String,
  url: String,
  created: Option[java.util.Date],
  modified: Option[java.util.Date],
  url: String,
  pic1: String,
  pic2: String,
  pic3: String,
  pic4: String,
  pic5: String,
  category_id: Int,
  platform_id: Int

}

Is this on the right track ?








On Wednesday, 6 June 2012 16:08:12 UTC+1, danny wrote:

johanandren

unread,
Jun 6, 2012, 1:05:51 PM6/6/12
to play-fr...@googlegroups.com
Looks about right to me. The strings are nullable so Option[String], unless you are really interested in distinguishing between no string and empty string I'd advise you to make those column NOT NULL as well, they could still be empty, so that should be ok. The foreign key id's in the end is also possibly null, so Option[Int] or Option[Long] there as well. 

You have seen the nice docs about anorm right?
Reply all
Reply to author
Forward
0 new messages