
springboot(sts)を使用してプログラミングをしています。
そこで、起動時に自動でテーブルを作成できるようにdata.sqlファイルを用意して作成しているのですが、data.sqlファイルで指定しているテーブル定義と実際に作成されるテーブル定義が異なって作成されてしまいます。なぜでしょうか?また解決方法をお教えください。
- ディレクトリ構成
- コード詳細
[Item.java]
java
1package com.example.demo.domain; 2 3import java.io.Serializable; 4 5import javax.persistence.Column; 6import javax.persistence.Entity; 7import javax.persistence.GeneratedValue; 8import javax.persistence.GenerationType; 9import javax.persistence.Id; 10import javax.persistence.Table; 11 12import lombok.AllArgsConstructor; 13import lombok.Data; 14import lombok.NoArgsConstructor; 15 16@Entity 17@Table(name = "items") 18@Data 19@NoArgsConstructor 20@AllArgsConstructor 21public class Item implements Serializable{ 22 @Id 23 @GeneratedValue(strategy = GenerationType.IDENTITY) 24 private Integer id; 25 private byte[] img; 26 private String title; 27 private String description; 28 private long price; 29}
[application.properties]
server.port=9003 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/testAPI?characterEncoding=UTF8&useSSL=false spring.datasource.username=root spring.datasource.password=7777 spring.jpa.hibernate.ddl-auto=update
[data.sql]
create table if not exists items( id int auto_increment primary key, img blob, title varchar(100), price bigint, description varchar(500));
- 実際のテーブル定義
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| description | varchar(255) | YES | | NULL | |
| img | tinyblob | YES | | NULL | |
| price | bigint(20) | NO | | NULL | |
| title | varchar(255) | YES | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
- 実現したいテーブル定義
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| img | blob | YES | | NULL | |
| title | varchar(100) | YES | | NULL | |
| price | bigint(20) | YES | | NULL | |
| description | varchar(500) | YES | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
よろしくお願いいたします。


回答1件
あなたの回答
tips
プレビュー