1

Im not sure what im doing wrong here i have read the spec but still cant figure it out.I want to add user id as a forign key to my store table but i keep getting error 1215.

My sql code:

CREATE TABLE Store (
id int,
storePersistentId varchar(255),
storename varchar(255),
user_id int,
PRIMARY KEY (id),
FOREIGN KEY (user_id) 
    REFERENCES user(id)
);

Store.java

@Entity
public class Store {

//every entity requires an id, and we can make it auto generated
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;

private String storePersistentId;
private String storename;

@ManyToOne
public User user;

public Store(User user,String storePersistentId){

}

public Store(){

}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getStorename() {
    return storename;
}

public void setStorename(String storename) {
    this.storename = storename;
}

public User getUser() {
    return user;
}

public void setUser(User user) {
    this.user = user;
}

public String getStorePersistentId() {
    return storePersistentId;
}

public void setStorePersistentId(String storePersistentId) {
    this.storePersistentId = storePersistentId;
}


}

User.java

@Entity
public class User {

@Id
@Column
private int id;

private String username;
private String password;


public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public User(String username, String password) {
    this.username = username;
    this.password = password;
}


public User() {
}
}

Any help would be great. its probally some small mistake but i need another set of eyes to check it out please.

college1
  • 23
  • 5
  • Do you have data in the Store table, is it possible that you have an id that doesn't correspond to an id in the user table? – Juan Dec 06 '17 at 20:26
  • Theres no data in the store table as i am tring to create it but i am not able to . i keep ketting the error 1215 when i try to create store table – college1 Dec 06 '17 at 20:37
  • Can you add User table as you did with Store? – Juan Dec 06 '17 at 20:57

1 Answers1

0
mysql> create table store(id int primary key NOT NULL AUTO_INCREMENT,
username varchar(255), 
password varchar(255),
user_id int, foreign key(user_id) 
references user(id));

Got it eventually.

college1
  • 23
  • 5