Skip to content

Commit 0108d3d

Browse files
committed
update mysql.sql
1 parent bb57397 commit 0108d3d

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

src/main/java/io/github/ngbsn/generator/associations/OneToManyMappingsGenerator.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public static void addBiDirectionalMappings(final Table table, final ForeignKeyC
5858
//Adding JoinColumn and MapsId annotations in below logic
5959
//Case: Composite Foreign key
6060
if (foreignKeyConstraint.getColumns().size() > 1) {
61-
handleCompositeForeignKey(table, foreignKeyConstraint, parentTableField, embeddableId, allPrimaryKeyColumns);
61+
handleCompositeForeignKey(table, parentTable, foreignKeyConstraint, parentTableField, embeddableId, allPrimaryKeyColumns);
6262

6363
}
6464
//Case: Single Foreign Key
@@ -108,15 +108,15 @@ private static void handleSingleForeignKey(final Table table, final ForeignKeyCo
108108
.build().toString());
109109
}
110110

111-
private static void handleCompositeForeignKey(final Table table, final ForeignKeyConstraint foreignKeyConstraint, final Column parentTableField,
112-
final EmbeddableClass embeddableId, final List<Column> allPrimaryKeyColumns) {
111+
private static void handleCompositeForeignKey(final Table table, final Table parentTable, final ForeignKeyConstraint foreignKeyConstraint,
112+
final Column parentTableField, final EmbeddableClass embeddableId, final List<Column> allPrimaryKeyColumns) {
113113
List<Column> listOfForeignKeyColumns = listOfForeignKeys(table, foreignKeyConstraint);
114114
//Case: Shared Composite Primary Key
115115
//If composite foreign key is inside the composite primary key, don't remove them from table.
116116
//This case assumes there is a primary composite key
117117
//Add a @MapsId annotation to the referenced table field
118118
if (embeddableId != null && new HashSet<>(allPrimaryKeyColumns).containsAll(listOfForeignKeyColumns)) {
119-
handleSharedCompositePrimaryKey(table, parentTableField, embeddableId, listOfForeignKeyColumns);
119+
handleCompositeForeignKeyInsideCompositePrimaryKey(table, parentTable, parentTableField, embeddableId, listOfForeignKeyColumns);
120120
} else {
121121
//Case1: There is no Composite primary key
122122
//TODO can part of Composite foreign key be a primary key. Is this applicable only to self referencing cases?
@@ -136,8 +136,8 @@ private static void handleCompositeForeignKey(final Table table, final ForeignKe
136136
parentTableField.getAnnotations().add(JoinColumnsAnnotation.builder().joinColumns(joinColumns).build().toString());
137137
}
138138

139-
private static void handleSharedCompositePrimaryKey(final Table table, final Column parentTableField,
140-
final EmbeddableClass embeddableId, final List<Column> listOfForeignKeyColumns) {
139+
private static void handleCompositeForeignKeyInsideCompositePrimaryKey(final Table table, final Table parentTable, final Column parentTableField,
140+
final EmbeddableClass embeddableId, final List<Column> listOfForeignKeyColumns) {
141141

142142
EmbeddableClass foreignCompositeKeyEmbedded = new EmbeddableClass(); //Create a new embeddable for this foreign composite key
143143
String embeddableName = listOfForeignKeyColumns.stream().map(Column::getFieldName).collect(Collectors.joining());

src/main/java/io/github/ngbsn/generator/models/ModelGenerator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ private static void processAlterTableStatements(final List<Statement> statements
157157
table.getColumns().removeIf(column -> column.getColumnName().equals(alterExpression.getColumnName()));
158158

159159
} else if (alterExpression.getOperation().name().equals("ADD")){
160+
//TODO check why alterExpression.getColDataTypeList() is a list ?
160161
String cName = alterExpression.getColDataTypeList().get(0) != null ? alterExpression.getColDataTypeList().get(0).getColumnName() : null;
161162
String columnName = alterExpression.getColumnName() != null ? alterExpression.getColumnName() : cName;
162163
String type = alterExpression.getColDataTypeList().get(0) != null ?

src/test/resources/sql/mysql.sql

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ CREATE TABLE organization.contractors (
88
);
99

1010
CREATE TABLE organization.employees (
11-
emp_no INT NOT NULL,
11+
emp_no INT NOT NULL AUTO_INCREMENT,
1212
birth_date DATE NOT NULL,
1313
first_name VARCHAR(14) NOT NULL,
1414
last_name VARCHAR(16) NOT NULL,
@@ -18,14 +18,14 @@ CREATE TABLE organization.employees (
1818
);
1919

2020
CREATE TABLE organization.departments (
21-
dept_no CHAR(4) NOT NULL,
22-
dept_name VARCHAR(40) NOT NULL,
21+
dept_no VARCHAR(100) NOT NULL,
22+
dept_name VARCHAR(100) NOT NULL,
2323
PRIMARY KEY (dept_no),
2424
UNIQUE (dept_name)
2525
);
2626

2727
CREATE TABLE organization.dept_manager (
28-
dept_no CHAR(4) NOT NULL,
28+
dept_no VARCHAR(100) NOT NULL,
2929
emp_no INT NOT NULL,
3030
FOREIGN KEY (emp_no) REFERENCES organization.employees (emp_no) ON DELETE CASCADE,
3131
FOREIGN KEY (dept_no) REFERENCES organization.departments (dept_no) ON DELETE CASCADE,
@@ -36,7 +36,7 @@ CREATE INDEX dept_manager_dept_no_idx ON organization.dept_manager(dept_no);
3636

3737
CREATE TABLE organization.dept_emp (
3838
emp_no INT NOT NULL,
39-
dept_no CHAR(4) NOT NULL,
39+
dept_no VARCHAR(100) NOT NULL,
4040
from_date DATE NOT NULL,
4141
to_date DATE NOT NULL,
4242
FOREIGN KEY (emp_no) REFERENCES organization.employees (emp_no) ON DELETE CASCADE,
@@ -71,4 +71,12 @@ CREATE TABLE organization.contractors_salaries (
7171
salary INT NOT NULL,
7272
FOREIGN KEY (contractor_no, first_name) REFERENCES organization.contractors (contractor_no, first_name) ON DELETE CASCADE,
7373
PRIMARY KEY (contractor_no, first_name, salary)
74+
);
75+
76+
CREATE TABLE organization.contractors_roles (
77+
contractor_no INT NOT NULL,
78+
first_name VARCHAR(14) NOT NULL,
79+
role VARCHAR(14) NOT NULL,
80+
FOREIGN KEY (contractor_no, first_name) REFERENCES organization.contractors (contractor_no, first_name) ON DELETE CASCADE,
81+
PRIMARY KEY (contractor_no, first_name)
7482
);

0 commit comments

Comments
 (0)