Once the foreign key definitions have been added in step 1, the CREATE TABLE statements will likely need to be reordered so that they execute without any errors. If there happens to be a circular foreign key dependency please resolve it using this method.
Most likely this will require a Postgres instance or the use of sqlfiddle to test that it is correct.
Example:
In the example in step 2, we added a foreign key from "accounthistory.createdby_d" to "user.id". However, as it stands in the current DDL script, the "user" table is created after the "accounthistory" table. Therefore, the CREATE TABLE statements for these tables need to be reordered as follows:
CREATE TABLE salesforce.user(
id VARCHAR(18) PRIMARY KEY,
username VARCHAR(240),
lastname VARCHAR(240),
...
)
;
CREATE TABLE salesforce.accounthistory(
id VARCHAR(18) PRIMARY KEY,
isdeleted BOOLEAN,
accountid VARCHAR(18) references salesforce.account(id),
createdbyid VARCHAR(18) references salesforce.user(id),
createddate TIMESTAMP WITHOUT TIME ZONE,
field VARCHAR(765),
oldvalue VARCHAR(765),
newvalue VARCHAR(765)
)
;