liquibase.exception.DatabaseException: Error executing SQL ALTER TABLE mysql.shoe_stype ADD CONSTRAINT cid FOREIGN KEY (shoe_cid) REFERENCES mysql.shoe (id): Can't create table '.\mysql\#sql-7b0_7.frm' (errno: 150)

609 views
Skip to first unread message

nitin chatra

unread,
Nov 6, 2014, 6:14:46 AM11/6/14
to dropwiz...@googlegroups.com
<?xml version="1.0" encoding="UTF-8"?>

<databaseChangeLog
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog

    <changeSet id="1" author="codahale">
        <createTable tableName="shoe">
            <column name="cid" type="bigint" autoIncrement="true">
              <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="cname" type="varchar(255)">
                <constraints nullable="false"/>
            </column>
            <column name="location" type="varchar(255)"/>
        </createTable>
    </changeSet>
    <changeSet id="2" author="chatra">
      <createTable tableName="stype">
           <column name="sid" type="bigint" autoIncrement="true">
               <constraints primaryKey="true" nullable="false"/>
               </column>
               <column name="type" type="varchar(255)">
                <constraints nullable="false"/>
                </column>
                </createTable>
      </changeSet>
     <changeSet id="3" author="guldu">
       
           <createTable tableName="shoe_stype">
             <column name="shoe_cid" type="bigint" >
                  <constraints primaryKey="true" nullable="false"/>
                  </column>
                       <column name="stype_sid" type="bigint">
                       <constraints primaryKey="true" nullable="false"/>
                       </column>
             </createTable>
             </changeSet>
             <changeSet id="4" author="chatra">
              <addForeignKeyConstraint constraintName="cid" referencedTableName="shoe" baseColumnNames="shoe_cid" baseTableName="shoe_stype" referencedColumnNames="id"/>
             <addForeignKeyConstraint constraintName="sid" referencedTableName="stype" baseColumnNames="stype_sid" baseTableName="shoe_stype" referencedColumnNames="id"/>
             </changeSet>
               </databaseChangeLog>
i want to create many to many relation
2)below is my pojo(shoe table) class

@Entity
@Table(name = "shoe")
@NamedQueries({
        @NamedQuery(
                name = "com.example.helloworld.core.Person.findAll",
                query = "SELECT p FROM Person p"
        )
})
public class Person {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long cid;

    @Column(name = "cname", nullable = false)
    private String cname;

    @Column(name = "location", nullable = false)
    private String location;
    @ManyToMany(cascade = {CascadeType.ALL})
    @JoinTable(name="shoe_stype", joinColumns={@JoinColumn(name="shoe_cid")}, 
       inverseJoinColumns={@JoinColumn(name="stype_sid")}) 
   
    
    private Set<Stype> st = new HashSet<Stype>();

    public Set<Stype> getSt() {
return st;
}

public void setSt(Set<Stype> st) {
this.st = st;
}

public long Cid() {
        return cid;
    }

    public void setCid(long cid) {
        this.cid = cid;
    }

    public String getCname() {
        return cname;
    }

    public void setCnmae(String cname) {
        this.cname = cname;
    }

    public String getLocation() {
        return location;
    }

    public void setJobTitle(String location) {
        this.location = location;
    }
3)this is my stype(table=stype) class
@Entity
@Table(name="stype")
public class Stype {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long sid;
@Column(name="type",nullable=false)
private String type;
@ManyToMany(mappedBy="st")
private Set<Person> per = new HashSet<Person>();
public Set<Person> getPer() {
return per;
}
public void setPer(Set<Person> per) {
this.per = per;
}

}


Jochen Schalanda

unread,
Nov 6, 2014, 5:57:48 PM11/6/14
to dropwiz...@googlegroups.com
Hi,

it would be great if you could write one or two sentences about your
problems instead of just dumping your source code or a screen shot in
the mail body.

Regarding today's error:

> $ perror 150
> MySQL error code 150: Foreign key constraint is incorrectly formed

You might want to make sure that you're not using the MyISAM for your
MySQL tables which does not support foreign keys. Use InnoDB instead.


Cheers,
Jochen
Reply all
Reply to author
Forward
0 new messages