I am having some trouble adding a column to an existing join table. Below is how I want my join table to look like, but I am missing the tenant_id
column in the join table in my actual implementation.
Below is my code for my JPA implementation:
@Entity
@Table(name = "label")
public class Label {
@Column(name = "tenant_id")
private String tenant_id;
@Column(name = "label_id")
private String label_id;
@JsonIgnore
@ManyToMany(targetEntity = Report.class, cascade = { CascadeType.PERSIST, CascadeType.MERGE })
@JoinTable(name = "tagging", joinColumns = @JoinColumn(name = "label_id"), inverseJoinColumns = @JoinColumn(name = "report_id"))
private Set<Report> reports;
}
@Entity
@Table(name = "report")
public class Report {
@Column(name = "tenant_id")
private String tenant_id;
@Column(name = "report_id")
private String report_id;
@column(name = "created_by")
private String created_by;
@JsonIgnore
@ManyToMany(targetEntity = Label.class, cascade = { CascadeType.PERSIST, CascadeType.MERGE })
@JoinTable(name = "tagging", joinColumns = @JoinColumn(name = "report_id"), inverseJoinColumns = @JoinColumn(name = "label_id"))
private Set<Label> labels;
}
And the code basically puts the id
of the Report
class and Label
class into the tagging table in place of the report_id
and label_id
columns respectively. The issue I'm having is that I want to add the tenant_id
column to every existing entry in my tagging
table. I tried
ALTER TABLE tagging ADD COLUMN tenant_id varchar(255) NOT NULL;
but because I have pre-existing data, I get an error saying that the tenant_id
must have a default value. I want to somehow update every tagging
table with the following:
select tenant_id from report where report.id = tagging.report_id
, and perform this update the moment the values in my database are populated/created. Is there any way to do this? Any help would be appreciated. Thanks!