• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    해결됨

17강 Unique index or primary key violation

23.07.21 16:45 작성 조회수 1.13k

0

안녕하세요 홍팍님

17강 듣는 중 에러 발생으로 질문있습니다

강의 6분대 정도의 위 사진까지 진도를 나가고 http://localhost:8080/articles/에서 새 아티클을 생성하면

위 페이지가 나옵니다

에러문구는 아래와 같습니다

2023-07-21 16:32:25.979  INFO 18712 --- [nio-8080-exec-2] c.e.f.controller.ArticleController       : ArticleForm(id=null, title=DDDD, content=4444)
2023-07-21 16:32:25.980  INFO 18712 --- [nio-8080-exec-2] c.e.f.controller.ArticleController       : Article(id=null, title=DDDD, content=4444)
2023-07-21 16:32:26.032 DEBUG 18712 --- [nio-8080-exec-2] org.hibernate.SQL                        : 
    insert 
    into
        article
        (id, content, title) 
    values
        (default, ?, ?)
2023-07-21 16:32:26.034 TRACE 18712 --- [nio-8080-exec-2] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARCHAR] - [4444]
2023-07-21 16:32:26.034 TRACE 18712 --- [nio-8080-exec-2] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [VARCHAR] - [DDDD]
2023-07-21 16:32:26.037  WARN 18712 --- [nio-8080-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 23505, SQLState: 23505
2023-07-21 16:32:26.038 ERROR 18712 --- [nio-8080-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper   : Unique index or primary key violation: "PRIMARY KEY ON PUBLIC.ARTICLE(ID) ( /* key:1 */ CAST(1 AS BIGINT), '1111', 'dummy1')"; SQL statement:
insert into article (id, content, title) values (default, ?, ?) [23505-214]
2023-07-21 16:32:26.056 ERROR 18712 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint ["PRIMARY KEY ON PUBLIC.ARTICLE(ID) ( /* key:1 */ CAST(1 AS BIGINT), '1111', 'dummy1')"; SQL statement:
insert into article (id, content, title) values (default, ?, ?) [23505-214]]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause

org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: Unique index or primary key violation: "PRIMARY KEY ON PUBLIC.ARTICLE(ID) ( /* key:1 */ CAST(1 AS BIGINT), '1111', 'dummy1')"; SQL statement:
insert into article (id, content, title) values (default, ?, ?) [23505-214]

아티클을 전부 다 지우고 몇개를 새로 생성하면 id값이 2번부터 생성돼요.

1, 2번 아티클을 지우고 새 아티클을 만들면 4번이 생성됩니다.

도와주십셔

 

스프링부트 버전

data.sql

build.gradle

plugins {
	id 'java'
	id 'org.springframework.boot' version '2.7.13'
	id 'io.spring.dependency-management' version '1.0.15.RELEASE'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'

java {
	sourceCompatibility = '11.0.19.7'
}

repositories {
	mavenCentral()
}

dependencies {
	// add lombok
	compileOnly 'org.projectlombok:lombok'
	annotationProcessor 'org.projectlombok:lombok'

	implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
	implementation 'org.springframework.boot:spring-boot-starter-mustache'
	implementation 'org.springframework.boot:spring-boot-starter-web'
	runtimeOnly 'com.h2database:h2'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'

	implementation fileTree(dir: 'lib', include: ['*.jar'])

}

tasks.named('test') {
	useJUnitPlatform()
}

 

application.property

 

 

답변 2

·

답변을 작성해보세요.

0

data.sql에서 적용된 id값이
자동증가 전략에 반영되지 않아 생긴 문제로 보입니다

data.sql을 다음과 같이 변경해보세요.

INSERT INTO ARTICLE(title, content) VALUES('dummy1', '1111');
INSERT INTO ARTICLE(title, content) VALUES('dummy2', '2222');
INSERT INTO ARTICLE(title, content) VALUES('dummy3', '3333');

 

0

tttzkq님의 프로필

tttzkq

질문자

2023.07.21

다른 질문을 보고 해결했습니다!