mysql - Delayed insert due to foreign key constraints -


i trying run query:

insert   `productstate` (`productid`, `changedon`, `state`) select t.`productid`, t.`processedon`, \'activated\'   `tmpimport` t   left join `product` p     on t.`productid` = p.`id`  p.`id` null     on duplicate key update        `changedon` = values(`changedon`) 

(i not quite sure query correct, appears working), running following issue. running query before creating entry 'products' table , getting foreign key constraint problem due fact entry not in products table yet.

my question is, there way run query, wait until next query (which updates product table) before performing insert portion of query above? note, if query run after product entry created no longer see p.id being null , therefore failing has performed before product entry created.

---> edit <--- concept trying achieve follows: starters importing set of data temp table, product table list of products (or have been in past) added through set of data temp table. need separate table provides state change product product become unavailable (no longer in data set provided vendor).

the productstate table follows:

create  table if not exists `productstate` (   `productid` varchar(32) not null ,   `changedon` date not null ,   `state` enum('activated','deactivated') null ,   primary key (`productid`, `changedon`) ,   index `fk_productstate_product` (`productid` asc) ,   constraint `fk_productstate_product`     foreign key (`productid` )     references `product` (`id` )     on delete no action     on update no action) engine = innodb default character set = utf8 collate = utf8_general_ci; 

the foreign key identifying relationship product table (product.id)

essentially trying accomplish this: 1. anytime new product (or deactivated product) shows in vendor data set, record created in productstate table 'activated'. 2. anytime product (that activated), not show in vendor data set, record created 'deactivated' in productstate table.

the purpose of productstate table track activation , deactivation states of product. productstate multi-to-one relationship product table, , state of product change once daily, therefore pkey productid , changeddate.

with foreign keys, need have data on product table first, before entering state, think logic: "how can dont exist have state" ?

so pseudocode of should do:

  1. read in vendor's product list
  2. compare them existing list in product table
  3. if new ones found: 3.1 insert product table, 3.2 insert productstate table
  4. if missing vendor's list: 4.1 insert productstate table

all these should done in 1 transaction. note should not delete things product table, unless want delete every information associated it, ie. delete "states" have stored.

rather trying in 1 query - best bet create stored procedure work step-by-step above. think gets overly complicated (or in case, impossible) in 1 query.

edit: this:

create procedure `some_procedure_name` () begin  -- breakdown tmpimport table 2 tables: new , removed select * _temp_new_products from`tmpimport` t left join `product` p on t.`productid` = p.`id` p.`id` null  select * _temp_removed_products `product` p left join `tmpimport` t  on t.`productid` = p.`id` t.`productid` null  -- each entry in _temp_new_products: -- 1. insert product table -- 2. insert productstate table 'activated'  -- each entry in _temp_removed_products: -- 1. insert productstate table 'deactivated'  -- drop temporary tables drop table _temp_new_products drop table _temp_removed_products end 

Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -