Raúl Peñate
Raúl Peñate

Reputation: 609

Entity not creating table in DB

I'm using Spring boot, and I Run this model.

package com.example.demo.Models;

import jakarta.persistence.*;

@Entity
@Table(name = "user")
public class UserModel {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(unique = true, nullable = true)
    private Long id;
    private String name;
    private String email;
    private Integer priority;
   
    /* Here are all the setters and getters*/
}

application.properties:

spring.datasource.url=jdbc:postgresql://localhost:5432/dbdemo
spring.datasource.username=postgres
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=update

Everything fine with Java.

Process finished with exit code 0

But in the console of my docker image of Postgres I get the following error:

ERROR:  syntax error at or near "user" at character 14
STATEMENT: create table user(id bigserial not null, email varchar(255), name varchar(255), priority integer, primary key (id))

I'm not sure how to solve it, I'd appreciate any help.

Upvotes: 2

Views: 744

Answers (3)

surendra ingle
surendra ingle

Reputation: 39

user is a reserved word in Postgresql thats why it show an error use a different word like "users" "usersId".

Upvotes: 2

You need to specify the schema name in table and id fields like this:

@Table(name = "anomaly", schema = "schema_name")
public class Anomaly {

@Id
@SequenceGenerator(name = "id",  allocationSize = 1, schema = "schema_name")
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long anomalyId;

Upvotes: 1

WiatroBosy
WiatroBosy

Reputation: 1124

create table public.user ( id serial not null, 
               email varchar(255), 
               name varchar(255), 
               priority integer, 
               primary key (id))

add the schema name before the table name

enter image description here

Upvotes: 2

Related Questions