与外键约束有困难。

huangapple go评论69阅读模式
英文:

Struggling with a foreign key constraint

问题

我在定义一些外键时遇到了问题
我有一个发行者Issuer),每个发行者有一个地址Address)。
我有一个地址Address),每个地址有一个国家Country)。

我试图在这些POJO上使用Liquibase定义外键约束但是没有成功仍然出现相同的错误

Caused by: liquibase.exception.DatabaseException: Column "COUNTRY_ID" not found; SQL statement:
ALTER TABLE PUBLIC.ADDRESS ADD CONSTRAINT FK_ADDRESS_TO_COUNTRY FOREIGN KEY (COUNTRY_ID) REFERENCES PUBLIC.COUNTRY (ID) [42122-200] [Failed SQL: (42122) ALTER TABLE PUBLIC.ADDRESS ADD CONSTRAINT FK_ADDRESS_TO_COUNTRY FOREIGN KEY (COUNTRY_ID) REFERENCES PUBLIC.COUNTRY (ID)]

这是我的类

@Entity
@Table(name = "COUNTRY")
@SequenceGenerator(name = "seq_country", sequenceName = "SEQ_COUNTRY", allocationSize = 1)
public class Country {
    // ...(略去其他代码)
}

@Entity
@Table(name = "ADDRESS")
@SequenceGenerator(name = "seq_address", sequenceName = "SEQ_ADDRESS", allocationSize = 1)
public class Address {
    // ...(略去其他代码)
}

@Entity
@Table(name = "ISSUER")
@SequenceGenerator(name = "seq_issuer", sequenceName = "SEQ_ISSUER", allocationSize = 1)
public class Issuer {
    // ...(略去其他代码)
}

<changeSet id="sremy" author="DEV-194-01">
    <dropColumn tableName="ASSET" columnName="LEGAL_ADDRESS"/>
    <dropColumn tableName="ASSET" columnName="MAIL_ADDRESS"/>
    <dropColumn tableName="ASSET" columnName="COMPANY_CONTACT_REF"/>
    <dropColumn tableName="ASSET" columnName="OTHER_CONTACT_REF"/>
    <dropColumn tableName="ASSET" columnName="COMPANY"/>
    <dropColumn tableName="ASSET" columnName="COUNTRY"/>
</changeSet>
<changeSet id="sremy" author="DEV-194-02">
    <createTable tableName="COUNTRY">
        <!-- ...略去其他代码 -->
    </createTable>
    <!-- ...略去其他代码 -->
</changeSet>
<changeSet id="sremy" author="DEV-194-03">
    <createTable tableName="ADDRESS">
        <!-- ...略去其他代码 -->
    </createTable>
    <addForeignKeyConstraint baseTableName="ADDRESS" baseColumnNames="COUNTRY_ID"
                             constraintName="FK_ADDRESS_TO_COUNTRY" referencedTableName="COUNTRY"
                             referencedColumnNames="ID"/>
</changeSet>
<changeSet author="sremy" id="DEV-194-04">
    <createTable tableName="ISSUER">
        <!-- ...略去其他代码 -->
    </createTable>
    <addForeignKeyConstraint baseTableName="ISSUER" baseColumnNames="ADDRESS_ID"
                             constraintName="FK_ISSUER_TO_ADDRESS" referencedTableName="ADDRESS"
                             referencedColumnNames="ID"/>
</changeSet>
<changeSet id="sremy" author="DEV-194-05">
    <addColumn tableName="ASSET">
        <!-- ...略去其他代码 -->
    </addColumn>
    <addForeignKeyConstraint
            baseTableName="ASSET" baseColumnNames="ISSUER_ID"
            referencedTableName="ISSUER" referencedColumnNames="ID"
            constraintName="FK_ASSET_TO_ISSUER"/>
</changeSet>

如果有人遇到了相同的问题,或者在这里看到了什么问题,请不要犹豫!感谢,Stephanie


<details>
<summary>英文:</summary>
I do have a problem defining some foreign keys. 
I have an Issuer, who have one address. 
I have an Address who have one country.
I&#39;m trying to define my foreign keys constraints with liquibse on those POJO, but with no luck, still same error: 
Caused by: liquibase.exception.DatabaseException: Column &quot;COUNTRY_ID&quot; not found; SQL statement:
ALTER TABLE PUBLIC.ADDRESS ADD CONSTRAINT FK_ADDRESS_TO_COUNTRY FOREIGN KEY (COUNTRY_ID) REFERENCES PUBLIC.COUNTRY (ID) [42122-200] [Failed SQL: (42122) ALTER TABLE PUBLIC.ADDRESS ADD CONSTRAINT FK_ADDRESS_TO_COUNTRY FOREIGN KEY (COUNTRY_ID) REFERENCES PUBLIC.COUNTRY (ID)]
Here are my classes 
@Entity
@Table(name = &quot;COUNTRY&quot;)
@SequenceGenerator(name = &quot;seq_country&quot;, sequenceName = &quot;SEQ_COUNTRY&quot;, allocationSize = 1)
public class Country {
@Id
@Column(name = &quot;ID&quot;)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = &quot;seq_country&quot;)
private Long id;
@Column(name = &quot;SHORT_NAME&quot;)
private String shortName;
@Column(name = &quot;NAME&quot;)
private String name;
@OneToMany(mappedBy = &quot;country&quot;, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List&lt;Address&gt; addresses;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getShortName() {
return shortName;
}
public void setShortName(String shortName) {
this.shortName = shortName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List&lt;Address&gt; getAddresses() {
return addresses;
}
public void setAddresses(List&lt;Address&gt; addresses) {
this.addresses = addresses;
}
----------
@Entity
@Table(name = &quot;ADDRESS&quot;)
@SequenceGenerator(name = &quot;seq_address&quot;, sequenceName = &quot;SEQ_ADDRESS&quot;, allocationSize = 1)
public class Address {
@Id
@Column(name = &quot;ID&quot;)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = &quot;seq_address&quot;)
private Long id;
@Column(name = &quot;ADDRESS&quot;)
private String address;
@Column(name = &quot;POSTAL_CODE&quot;)
private String postalCode;
@Column(name = &quot;CITY&quot;)
private String city;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = &quot;COUNTRY_ID&quot;)
private Country country;
@OneToOne(mappedBy = &quot;address&quot;)
private Issuer issuer;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPostalCode() {
return postalCode;
}
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public Country getCountry() {
return country;
}
public void setCountry(Country country) {
this.country = country;
}
public Issuer getIssuer() {
return issuer;
}
public void setIssuer(Issuer issuer) {
this.issuer = issuer;
}
----------
@Entity
@Table(name = &quot;ISSUER&quot;)
@SequenceGenerator(name = &quot;seq_issuer&quot;, sequenceName = &quot;SEQ_ISSUER&quot;, allocationSize = 1)
public class Issuer {
@Id
@Column(name = &quot;ID&quot;)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = &quot;seq_issuer&quot;)
private Long id;
@Column(name = &quot;NAME&quot;)
private String name;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = &quot;ADDRESS_ID&quot;)
private Address address;
@Column(name = &quot;MAIL_ADDRESS&quot;)
private String emailAddress;
@Column(name = &quot;COMPANY_CONTACT_REF&quot;)
private String contactPerson;
@Column(name = &quot;OTHER_CONTACT_REF&quot;)
private String otherContactPerson;
@OneToMany(mappedBy = &quot;issuer&quot;, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private Collection&lt;Asset&gt; assets;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
public String getContactPerson() {
return contactPerson;
}
public void setContactPerson(String contactPerson) {
this.contactPerson = contactPerson;
}
public String getOtherContactPerson() {
return otherContactPerson;
}
public void setOtherContactPerson(String otherContactPerson) {
this.otherContactPerson = otherContactPerson;
}
public Collection&lt;Asset&gt; getAssets() {
return assets;
}
public void setAssets(Collection&lt;Asset&gt; assets) {
this.assets = assets;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
----------
&lt;changeSet id=&quot;sremy&quot; author=&quot;DEV-194-01&quot;&gt;
&lt;dropColumn tableName=&quot;ASSET&quot; columnName=&quot;LEGAL_ADDRESS&quot;/&gt;
&lt;dropColumn tableName=&quot;ASSET&quot; columnName=&quot;MAIL_ADDRESS&quot;/&gt;
&lt;dropColumn tableName=&quot;ASSET&quot; columnName=&quot;COMPANY_CONTACT_REF&quot;/&gt;
&lt;dropColumn tableName=&quot;ASSET&quot; columnName=&quot;OTHER_CONTACT_REF&quot;/&gt;
&lt;dropColumn tableName=&quot;ASSET&quot; columnName=&quot;COMPANY&quot;/&gt;
&lt;dropColumn tableName=&quot;ASSET&quot; columnName=&quot;COUNTRY&quot;/&gt;
&lt;/changeSet&gt;
&lt;changeSet id=&quot;sremy&quot; author=&quot;DEV-194-02&quot;&gt;
&lt;createTable tableName=&quot;COUNTRY&quot;&gt;
&lt;column name=&quot;ID&quot; type=&quot;BIGINT&quot;&gt;
&lt;constraints nullable=&quot;false&quot;/&gt;
&lt;/column&gt;
&lt;column name=&quot;CODE_ALPHA_2&quot; type=&quot;VARCHAR(2)&quot;/&gt;
&lt;column name=&quot;NAME&quot; type=&quot;VARCHAR(50)&quot;/&gt;
&lt;/createTable&gt;
&lt;addPrimaryKey tableName=&quot;COUNTRY&quot; columnNames=&quot;ID&quot;
constraintName=&quot;PK_COUNTRY&quot;/&gt;
&lt;createSequence sequenceName=&quot;SEQ_COUNTRY&quot;/&gt;
&lt;/changeSet&gt;
&lt;changeSet id=&quot;sremy&quot; author=&quot;DEV-194-03&quot;&gt;
&lt;createTable tableName=&quot;ADDRESS&quot;&gt;
&lt;column name=&quot;ID&quot; type=&quot;BIGINT&quot;&gt;
&lt;constraints nullable=&quot;false&quot;/&gt;
&lt;/column&gt;
&lt;column name=&quot;ADDRESS&quot; type=&quot;VARCHAR(250)&quot;/&gt;
&lt;column name=&quot;POSTAL_CODE&quot; type=&quot;VARCHAR(20)&quot;/&gt;
&lt;column name=&quot;CITY&quot; type=&quot;VARCHAR(250)&quot;/&gt;
&lt;/createTable&gt;
&lt;addPrimaryKey tableName=&quot;ADDRESS&quot; columnNames=&quot;ID&quot;
constraintName=&quot;PK_ADDRESS&quot;/&gt;
&lt;createSequence sequenceName=&quot;SEQ_ADDRESS&quot;/&gt;
&lt;addForeignKeyConstraint baseTableName=&quot;ADDRESS&quot; baseColumnNames=&quot;COUNTRY_ID&quot;
constraintName=&quot;FK_ADDRESS_TO_COUNTRY&quot; referencedTableName=&quot;COUNTRY&quot;
referencedColumnNames=&quot;ID&quot;/&gt;
&lt;/changeSet&gt;
&lt;changeSet author=&quot;sremy&quot; id=&quot;DEV-194-04&quot;&gt;
&lt;createTable tableName=&quot;ISSUER&quot;&gt;
&lt;column name=&quot;ID&quot; type=&quot;BIGINT&quot;&gt;
&lt;constraints nullable=&quot;false&quot;/&gt;
&lt;/column&gt;
&lt;column name=&quot;NAME&quot; type=&quot;VARCHAR(250)&quot;&gt;
&lt;/column&gt;
&lt;column name=&quot;MAIL_ADDRESS&quot; type=&quot;VARCHAR(250)&quot;&gt;
&lt;/column&gt;
&lt;column name=&quot;COMPANY_CONTACT_REF&quot; type=&quot;VARCHAR(250)&quot;&gt;
&lt;/column&gt;
&lt;column name=&quot;OTHER_CONTACT_REF&quot; type=&quot;VARCHAR(250)&quot;&gt;
&lt;/column&gt;
&lt;/createTable&gt;
&lt;addPrimaryKey tableName=&quot;ISSUER&quot; columnNames=&quot;ID&quot;
constraintName=&quot;PK_ISSUER&quot;/&gt;
&lt;createSequence sequenceName=&quot;SEQ_ISSUER&quot;/&gt;
&lt;addForeignKeyConstraint baseTableName=&quot;ISSUER&quot; baseColumnNames=&quot;ADDRESS_ID&quot;
constraintName=&quot;FK_ISSUER_TO_ADDRESS&quot; referencedTableName=&quot;ADDRESS&quot;
referencedColumnNames=&quot;ID&quot;/&gt;
&lt;/changeSet&gt;
&lt;changeSet id=&quot;sremy&quot; author=&quot;DEV-194-05&quot;&gt;
&lt;addColumn tableName=&quot;ASSET&quot;&gt;
&lt;column name=&quot;ISSUER_ID&quot; type=&quot;BIGINT&quot;&gt;
&lt;constraints nullable=&quot;false&quot;/&gt;
&lt;/column&gt;
&lt;/addColumn&gt;
&lt;addForeignKeyConstraint
baseTableName=&quot;ASSET&quot; baseColumnNames=&quot;ISSUER_ID&quot;
referencedTableName=&quot;ISSUER&quot; referencedColumnNames=&quot;ID&quot;
constraintName=&quot;FK_ASSET_TO_ISSUER&quot;/&gt;
&lt;/changeSet&gt;
&lt;/databaseChangeLog&gt;
If someone had the same problem, or see something awful in here, don&#39;t hesitate! 
Thanks, Stephanie
</details>
# 答案1
**得分**: 0
首先,首先,在ADDRESS表中您没有列名COUNTRY_ID,但您仍然在尝试使用此列创建外键
&lt;addForeignKeyConstraint baseTableName=&quot;ADDRESS&quot; baseColumnNames=&quot;COUNTRY_ID&quot;
constraintName=&quot;FK_ADDRESS_TO_COUNTRY&quot; 
referencedTableName=&quot;COUNTRY&quot;
referencedColumnNames=&quot;ID&quot;/&gt;

将基本列名从COUNTRY_ID更改为ID。
<details>
<summary>英文:</summary>
First of all you don&#39;t have Column name COUNTRY_ID in ADDRESS table but you are still trying to create foreign key using this column
&lt;addForeignKeyConstraint baseTableName=&quot;ADDRESS&quot; baseColumnNames=&quot;COUNTRY_ID&quot;
constraintName=&quot;FK_ADDRESS_TO_COUNTRY&quot; 
referencedTableName=&quot;COUNTRY&quot;
referencedColumnNames=&quot;ID&quot;/&gt;
change the base column name from COUNTRY_ID to ID 
</details>

huangapple
  • 本文由 发表于 2020年9月8日 21:57:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63795512.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定