0

I'm new to java . i've two questions . i'm using flyway and h2 db i added two file sql one of them to create table with two columns like that

CREATE TABLE contacts (
id bigint auto_increment NOT NULL, 
name varchar(128) NOT NULL,
PRIMARY KEY(id)
);

and the other is to alter new column like that

ALTER TABLE contacts
ADD COLUMN contacts Varchar(255);  

1- i used flyway.migrate worked fine but i faced mismatch so i used flyway.repair() is that normal to use it every time ?

2- when i wrote statment sql for executing insert sql command like that

stmt.execute("INSERT INTO contacts(name,contacts) VALUES('ABC','ABC@yahoo.com')");

i got

Exception in thread "main" org.h2.jdbc.JdbcSQLException: Column "CONTACTS" not found; SQL statement:
INSERT INTO contacts(name,contacts) VALUES('ABC ','ABC@yahoo.com') [42122-173]

1 Answers1

0

You need to add AFTER in your second sql file.

ALTER TABLE contacts
ADD COLUMN contacts Varchar(255) AFTER name;
John Joe
  • 12,412
  • 16
  • 70
  • 135
  • thanks for replying , it worked but execuse me , should i write after to make db know where it will add the new column ? –  Mar 15 '17 at 04:53
  • can you help in this http://stackoverflow.com/questions/42801000/no-data-is-available –  Mar 15 '17 at 04:54
  • I would be surprised if this was the actual solution, as the [`AFTER`-clause is **optional**](http://h2database.com/html/grammar.html#alter_table_add). I'd sooner expect something like transaction visibility of metadata changes. – Mark Rotteveel Mar 15 '17 at 08:28
  • yes, that is make me wonder really , but i tried both commands with after and without it and worked with it –  Mar 15 '17 at 20:26