java - Add column to Join table? -
i having trouble adding column existing join table. below how want join table like, missing tenant_id
column in join table in actual implementation.
below code 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 code puts id
of report
class , label
class tagging table in place of report_id
, label_id
columns respectively. issue i'm having want add tenant_id
column every existing entry in tagging
table. tried
alter table tagging add column tenant_id varchar(255) not null;
but because have pre-existing data, error saying tenant_id
must have default value. want somehow update every tagging
table following:
select tenant_id report report.id = tagging.report_id
, , perform update moment values in database populated/created. there way this? appreciated. thanks!
i'm not sure, did understand problem properly. if need add tenant_id
, fill can next.
first, add new column default null:
alter table tagging add column tenant_id varchar(255) default null;
second, update tenant_id:
update tagging t set t.tenant_id = (select r.tenant_id report r r.id = t.report_id);
third, modify tagging
table constraint:
alter table tagging modify column tenant_id varchar(255) not null;
note, must sure, report
table not containg null
value in tenant_id
column.
Comments
Post a Comment