sql - H2 and FOREIGN KEY -


i ran problem:

i going create 2 tables in h2. when tried create second table, error occured:

column c_task not found

here code:

create table s_task (c_task int primary key, n_task varchar(255), point_count int);  create table s_achievement(c_achievement int primary key, n_reward varchar(255), picture varchar(255), foreign key (c_task) references s_task(c_task), exec_count int); 

did not define linking field

you defining relationship (a foreign key) column not exist in table s_achievement. foreign key link between column on child table (the "many" table) , column on parent table (the "one" table). code says wish link field "c_task" on "s_achievement" there no "c_task" field on table.

example

take example customers (parent table) have 0 or more invoices (child table), , every invoice must owned 1 customer. similarly, invoice table in turn parent line items table.

diagram of 3 tables, invoice_ linking customer_, , line_item_ linking invoice_

you have tables , columns:

  • customer_
    • name_
    • phone_
    • uuid_ (primary key)
  • invoice_
    • invoice_number_
    • date_of_invoice_
    • uuid_ (primary key)
    • fk_customer_uuid_ (foreign key)
  • line_item_
    • item_number_
    • product_sold_
    • cost_
    • uuid_ (primary key)
    • fk_invoice_uuid_ (foreign key)

on invoice_ table define:

alter table invoice_ add foreign key ( fk_customer_uuid_ ) references customer_( uuid_ ) ; 

and

alter table line_item_ add foreign key ( fk_invoice_uuid_ ) references invoice_( uuid_ ) ; 

those columns marked "(foreign key)" forgot define on child table.

see posting, h2 alter table add foreign key, example.


Comments

Popular posts from this blog

How to connect android app to App engine -

gcc - MinGW's ld cannot perform PE operations on non PE output file -

php - display validation error message next to the textbox in codeigniter -