I have a project with spring boot starter validations and recently added the driver to connect to a postgres database and the ddl-auto=create. When the application starts it is not able to create my table. I have two models with the @Entity annotation, one doesn’t have validations and is succesfully created, but the other(Usuarios) does have them. I have used the same code but with h2 and everything seemed to work fine, but when I changed to Postgres I get this error printed while initializing the project:
Hint: Ningún operador coincide en el nombre y tipos de argumentos. Puede ser necesario agregar conversión explícita de tipos. Error executing DDL "create table usuarios (id bigserial not null, contrasena varchar(255) not null, correo varchar(255) not null, imagen varchar(255) not null check (imagen>=1 AND imagen<=7), nombre varchar(70) not null, telefono varchar(10) not null, primary key (id))" via JDBC Statement
Caused by: org.postgresql.util.PSQLException: ERROR: el operador no existe: character varying >= integer
Here is the model with issues:
@Entity
@Table(name="Usuarios")
public class Usuarios {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(unique=true, nullable=false)
private Long id;
@NotNull
@Size(min=5, max=70)
@Column(nullable=false)
private String nombre;
@NotNull
@Size(min=10, max=10)
@Column(nullable=false)
private String telefono;
@NotNull
@Email
@Column(nullable=false)
private String correo;
@Size(min=6)
@Column(nullable=false)
private String contrasena;
@NotNull
@Min(1)
@Max(7)
@Column(nullable=false)
private String imagen;
2
Answers
The check constraint is wrong:
imagen is a piece of text, that can’t be larger or equal to 1. Nor can it be smaller or equal to 7. It’s text, not an integer.
What should the constraint do?
You can use @Size or @Length instead of @Min and @Max anatations. @Min and @Max annotations are often used in numeric data types.
For example:
or
You can check Difference Between @Size, @Length this link.