Spring Boot java.sql.SQLSyntaxErrorException: Unknown column 에러 해결

ovovvv·2023년 2월 1일
1

스프링 부트에 MySQL 연결해서 테스트 하던 도중 예상치 않은 에러가 나왔다

java.sql.SQLSyntaxErrorException: Unknown column 'user_id' in 'field list'

에러 자체는 흔한 오류였다
DB에 user_id 라는 컬럼이 없어서 SQL 명령어 실행이 실패 했다고 한다


그야 당연히 없으니까...

@Getter
@NoArgsConstructor
@Entity
@Table(name = "test")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY) //SpringBoot 2.0 부터 auto increment 위해서 필수
    @Column(name = "_id", unique = true, nullable = false)
    private Long id;

    @Column(name = "userId", unique = true, nullable = false)
    private String userId;

    @Column(name = "password", nullable = false)
    private String password;

    @Column(name = "name", nullable = false)
    private String name;

    @Column(name = "nickname", unique = true, nullable = false)
    private String nickname;

    @Column(name = "phone", unique = true, nullable = false)
    private String phone;

    @Column(name = "birth")
    private String birth;

    @Column(name = "gender")
    private String gender;


    @Builder
    public User(String userId, String password, String name, String nickname, String phone, String birth, String gender) {
        this.userId = userId;
        this.password = password;
        this.name = name;
        this.nickname = nickname;
        this.phone = phone;
        this.birth = birth;
        this.gender = gender;
    }
}

문제는 내가 쓴 코드 어디에도 user_id가 없는데
왜 저기다 쑤셔넣으려고 하냐고...

그 이유는
Linux나 Unix 환경의 DB는 대소문자에 영향을 받지만
Windows는 영향을 받지 않는다고 한다

https://dev.mysql.com/doc/refman/8.0/en/identifier-case-sensitivity.html

By default, table aliases are case-sensitive on Unix, but not so on Windows or macOS. The following statement would not work on Unix, because it refers to the alias both as a and as A:

However, this same statement is permitted on Windows. To avoid problems caused by such differences, it is best to adopt a consistent convention, such as always creating and referring to databases and tables using lowercase names. This convention is recommended for maximum portability and ease of use.

이 때문에 jpa가 호환성을 위해서 userId처럼 카멜 표기 한걸
user_id로 자동 변경하도록 되어있어 생긴 문제인듯...


DB 컬럼이름을 바꿔주니까 잘들어갑니다 허허

0개의 댓글