1

I'm trying to persist objects to a postgresql Db inside my play2 webapp using anorm library. The Create statement of the table "Address" is this one:

create table address (
    addressId               bigserial not null,
    addressName             varchar(255) not null,
    street                  varchar(255) not null,
    number                  varchar(15) not null,
    ZIP                     varchar(15) not null,
    region                  varchar(63) not null,
    country                 varchar(63) not null,
    constraint pk_address primary key (addressId)
);

And my Scala/Anorm code looks like this:

private def create(address: Address, recursive : Boolean): Long = {
    DB.withConnection { implicit connection =>
        SQL( """
             insert into {table_name} (addressName, street, number, ZIP, region, country)
             values ({name},{street},{number},{ZIP},{region},{country})
            """).on(
            "table_name" -> TABLE_NAME,
            "name" -> address.name,
            "street" -> address.street,
            "number" -> address.number,
            "ZIP" -> address.ZIP,
            "region" -> address.region,
            "country"-> address.country
        ).executeInsert[Option[Long]]().get
    }
}

Obviously TABLE_NAME is a val that contains the string "Address"

The problem is that when i try to execute it with a proper Address instance like:

Address("Antonio","vacchi","12","48012","RA","Italia")

I get this exception:

[PSQLException: ERROR: syntax error at or near "$1" Posizione: 13] 

'Posizione' is the italian word for Position (dunno why i get the error in italian o:)

1 Answer 1

2

Using the interpolation with the {table_name} syntax will result in the SQL containing a positioned parameter. This is something that is not supported when it comes to CREATE statements. So in this case you have to write out Address and not pass it in the on parameters.

Sign up to request clarification or add additional context in comments.

1 Comment

You saved my day/night ;) Don't know why I wasn't thinking at it like a pretty wrapper of prepared Statements ;) Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.