Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8952811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:57:25+00:00 2026-06-15T13:57:25+00:00

I’m trying to understand how Slick works and how to use it… and looking

  • 0

I’m trying to understand how Slick works and how to use it… and looking at their examples in GitHub I ended up with this code snippet in MultiDBCakeExample.scala:

trait PictureComponent { this: Profile => //requires a Profile to be mixed in...
  import profile.simple._ //...to be able import profile.simple._

  object Pictures extends Table[(String, Option[Int])]("PICTURES") {
    ...

    def * = url ~ id

    val autoInc = url returning id into { case (url, id) => Picture(url, id) }

    def insert(picture: Picture)(implicit session: Session): Picture = {
      autoInc.insert(picture.url)
    }
  }
}

I suppose the * method returns a row in the table, while autoInc should somehow provide functionality for automatically incrementing the entity id… but to be honest I’ve some trouble in understanding this piece of code. What does returning refer to? What does autoInc return?

I looked at the Slick documentation but I was unable to find helpful information. Any help would be really appreciated 😉

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:57:26+00:00Added an answer on June 15, 2026 at 1:57 pm

    Because that autoInc can be confusing I will provide you a working example (please note that my DB is PostgreSQL so I need that hack with forInsert in order to make Postgresql driver increment auto-inc values).

    case class GeoLocation(id: Option[Int], latitude: Double, longitude: Double, altitude: Double)
    
    /**
     * Define table "geo_location".
     */
    object GeoLocations extends RichTable[GeoLocation]("geo_location") {
      def latitude = column[Double]("latitude")
      def longitude = column[Double]("longitude")
      def altitude = column[Double]("altitude")
    
      def * = id.? ~ latitude ~ longitude ~ altitude <> (GeoLocation, GeoLocation.unapply _)
      def forInsert = latitude ~ longitude ~ altitude <> ({ (lat, long, alt) => GeoLocation(None, lat, long, alt) },
        { g: GeoLocation => Some((g.latitude, g.longitude, g.altitude)) })
    }
    

    My RichTable is an abstract class in order to not declare ids for each Table I have but just extend this:

    abstract class RichTable[T](name: String) extends Table[T](name) {
      def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
    
      val byId = createFinderBy(_.id)
    
    }
    

    and use it like:

    GeoLocations.forInsert.insert(GeoLocation(None, 22.23, 25.36, 22.22))
    

    Since you pass None for id, it will be generated automatically by PostgreSql driver when Slick inserts this new entity.
    I have a few weeks since I started with Slick and I really recommend it!

    UPDATE: If you want to not use forInsert projections, another approach is the following – in my case the entity is Address.

    Create sequence for each table on schema creation:

    session.withTransaction {
      DBSchema.tables.drop
      DBSchema.tables.create
      // Create schemas to generate ids too.
      Q.updateNA("create sequence address_seq")
    }
    

    Define a method to generate the ids using sequences (I defined this once in RichTable class:

      def getNextId(seqName: String) = Database { implicit db: Session =>
        Some((Q[Int] + "select nextval('" + seqName + "_seq') ").first)
      }
    

    and in the mapper override insert method like:

      def insert(model : Address) = Database { implicit db: Session =>
        *.insert(model.copy(id = getNextId(classOf[Address].getSimpleName())))
      }
    

    And now, you can pass None when you do an insert and this methods will do a nice work for you…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.