Using more then one autoincrement field in postgres with squeryl

44 views
Skip to first unread message

Sanjarbek Amatov

unread,
Dec 21, 2014, 12:16:40 PM12/21/14
to squ...@googlegroups.com
is it possible to using more than one autoincremented field with squeryl? database is postgresql.

David Whittaker

unread,
Dec 22, 2014, 1:20:48 PM12/22/14
to squ...@googlegroups.com

Yes, it is possible. If you’re having Squeryl generate the database schema for you, use the is autoIncremented column attribute. See the documentation for more info. Either way Squeryl should retrieve the db generated value and update your model object when the row is inserted.


On Sun, Dec 21, 2014 at 12:16 PM, Sanjarbek Amatov <asanj...@gmail.com> wrote:
is it possible to using more than one autoincremented field with squeryl? database is postgresql.

--
You received this message because you are subscribed to the Google Groups "Squeryl" group.
To unsubscribe from this group and stop receiving emails from it, send an email to squeryl+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sanjarbek Amatov

unread,
Dec 22, 2014, 9:54:44 PM12/22/14
to squ...@googlegroups.com
I am using play! (2.1 version). To generate sql I am using play2! evolutions. Database is postgres. Squeryl library version is 0.9.5-6.

I have this two tables, orders and employment_orders. employment_orders table inherits orders table. The code is below
CREATE TABLE orders (
  id             BIGSERIAL PRIMARY KEY,
  order_type_id INT REFERENCES order_types (id),
  date_of_order  DATE,
  created_at    TIMESTAMP NOT NULL,
  updated_at    TIMESTAMP NOT NULL
);


CREATE TABLE employment_orders (
  position_id        INT,
  contract_type_id   INT,
  contract_number BIGINT UNIQUE NOT NULL,
  employee_id        BIGINT,
  salary             NUMERIC(14, 2),
  calendar_type_id   SMALLINT,
  trial_period_start DATE,
  trial_period_end   DATE,
  start_date         DATE,
  end_date           DATE,
  close_date      DATE
)
  INHERITS (orders);


The case class for employment_orders table
case class EmploymentOrder(
                            id: Long,
                            order_type_id: Int,
                            date_of_order: Date,
                            position_id: Int,
                            contract_type_id: Int,
                            contract_number: Long,
                            employee_id: Long,
                            salary: BigDecimal,
                            calendar_type_id: Int,
                            trial_period_start: Option[Date],
                            trial_period_end: Option[Date],
                            start_date: Date,
                            end_date: Option[Date],
                            close_date: Option[Date],
                            override var created_at: TimeStamp,
                            override var updated_at: TimeStamp
                            ) extends Entity[Long]


Database schema 
object Database extends Schema {
...
...
val employmentOrderTable: Table[EmploymentOrder] = table[EmploymentOrder]("employment_orders")

on(employmentOrderTable) ( employmentOrder => declare (
    employmentOrder.id is (autoIncremented("orders_id_seq")),
    employmentOrder.contract_number is (autoIncremented("employment_orders_contract_number_seq"))
  ))
...
...
}

This is build.sbt file 
libraryDependencies ++= Seq(
  jdbc,
  cache,
  "org.squeryl" %% "squeryl" % "0.9.5-6",
  "postgresql" % "postgresql" % "9.2-1002.jdbc4",
  "org.webjars" %% "webjars-play" % "2.2.1",
  "org.webjars" % "bootstrap" % "3.1.1-2",
...other dependencies.
)

This is my ide screen where you can see error



how web browser see this error




вторник, 23 декабря 2014 г., 0:20:48 UTC+6 пользователь David Whittaker написал:

Sanjarbek Amatov

unread,
Dec 22, 2014, 10:01:00 PM12/22/14
to squ...@googlegroups.com
I looked at code of squeryl in github.
// Validate that autoIncremented is not used on other fields than KeyedEntity[A].id :
   // since it is not yet unsupported :
   for(ca <- colAss) ca match {
     case cga:CompositeKeyAttributeAssignment => {}
     case caa:ColumnAttributeAssignment => {
       for(ca <- caa.columnAttributes if ca.isInstanceOf[AutoIncremented] && !(caa.left.isIdFieldOfKeyedEntity))
         org.squeryl.internals.Utils.throwError("Field " + caa.left.nameOfProperty + " of table " + table.name +
               " is declared as autoIncremented, auto increment is currently only supported on KeyedEntity[A].id")
     }
     case dva:Any => {}
   }

Maybe I need to use different version of squeryl. My squeryl version is  0.9.5-6.

David Whittaker

unread,
Dec 22, 2014, 10:18:55 PM12/22/14
to squ...@googlegroups.com
Huh.  No, it turns out you are right.  I had thought the ability to get generated key info for arbitrary fields existed, but it seems like it's not available in master either.  Sorry about that, I guess it's only available for primary keys.

--

Sanjarbek Amatov

unread,
Dec 23, 2014, 9:00:38 AM12/23/14
to squ...@googlegroups.com
Thank you David for response. Ok, I will looking at another ways to do it.
Reply all
Reply to author
Forward
0 new messages