I use oci_execute with OCI_NO_AUTO_COMMIT so if an insert fails later on, the inserts simply wont happen. And if everyting runs successfully I make a commit at the end;
I run an insert on TABLE_A that returns the inserted rows ID that I want to use righ after. But since it hasn't been committed yet I get a foreign key violation error.
I don't know PHP, but - see if my Oracle side of view helps.
This is what you have now:
SQL> create table table_a (id number primary key);
Table created.
SQL> create table table_b (id number primary key,
2 id_fk number references table_a
3 );
Table created.
SQL> insert into table_b values (1, 1);
insert into table_b values (1, 1)
*
ERROR at line 1:
ORA-02291: integrity constraint (SCOTT.SYS_C00105535) violated - parent key not
found
Of course it won't work ... there's no parent key in table_a
.
But, if you change foreign key constraint so that it "ignores" whether referenced key exists or not and make it check it at COMMIT
point, this might be what you're looking for.
See lines #3 and #4:
SQL> drop table table_b;
Table dropped.
SQL> create table table_b (id number primary key,
2 id_fk number constraint fk_ba references table_a
3 initially deferred deferrable
4 );
Table created.
SQL> insert into table_b values (1, 1);
1 row created.
See? Insert succeeded, although referenced primary key in table_a
doesn't exist yet.
So, once you enter it as well ...
SQL> insert into table_a values (1);
1 row created.
... and commit ...
SQL> commit;
Commit complete.
SQL>
... everything is here:
SQL> select * From table_a;
ID
----------
1
SQL> select * from table_b;
ID ID_FK
---------- ----------
1 1
SQL>
If the problem is related to PHP, then I'm afraid I can't help.