Scalatra / Slick and insert IF NOT EXISTS

前端 未结 1 1801
离开以前
离开以前 2021-01-18 02:36

I am a newbie so hoping for some patience. :)

I am trying to populate two tables if a value does not exist. Basically I have:

TABLE b
(
    id VARCHA         


        
1条回答
  •  礼貌的吻别
    2021-01-18 03:31

    In Slick 1.0.1

    db.withTransaction{ implicit session : Session =>
      if( ! Query(b).filter(_.id==="something").exists.run ){
        b.insert( "something" )
        d.insert( (1,"something") )
      }
    }
    

    In Slick 2.0

    val b = TableQuery[b]
    db.withTransaction{ implicit session =>
      if( ! b.filter(_.id==="something").exists.run ){
        b += "something"
        d += (1,"something")
      }
    }
    

    0 讨论(0)
提交回复
热议问题