skip to Main Content

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


  1. The check constraint is wrong:

    imagen varchar(255) not null check (imagen>=1 AND imagen<=7)

    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?

    Login or Signup to reply.
  2. 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:

    @NotNull
    @Size(min = 1, max = 7)
    @Column(nullable=false)
    private String imagen
    

    or

    @NotNull
    @Length(min = 1, max = 7)
    @Column(nullable=false)
    private String imagen
    

    You can check Difference Between @Size, @Length this link.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search