Duplicate table names in WSO2 API monetization documentation for Postgres DB - wso2

I was going through the WSO2 documentation to Monetize an API. In the database configuration section, it has been mentioned that we have to execute the provided database script. If we select Postgres, it displays the script to be executed.
However, all the CREATE TABLE statements have the same table name, even though the sequence names are different. For other databases like MySQL, Oracle, etc, the table names are different as well.
Just to be sure, I even checked with older documentation and it's the same even in older documentation. Script for Postgres database:
CREATE SEQUENCE AM_MONETIZATION START WITH 1 INCREMENT BY 1;
CREATE TABLE IF NOT EXISTS AM_POLICY_SUBSCRIPTION (
API_ID INTEGER NOT NULL,
TIER_NAME VARCHAR(512),
STRIPE_PRODUCT_ID VARCHAR(512),
STRIPE_PLAN_ID VARCHAR(512),
FOREIGN KEY (API_ID) REFERENCES AM_API (API_ID) ON DELETE CASCADE
);
CREATE SEQUENCE AM_POLICY_PLAN_MAPPING START WITH 1 INCREMENT BY 1;
CREATE TABLE IF NOT EXISTS AM_POLICY_SUBSCRIPTION (
POLICY_ID INTEGER DEFAULT NEXTVAL('AM_POLICY_PLAN_MAPPING'),
POLICY_UUID VARCHAR(256),
PRODUCT_ID VARCHAR(512),
PLAN_ID VARCHAR(512),
FOREIGN KEY (POLICY_UUID) REFERENCES AM_POLICY_SUBSCRIPTION(UUID)
);
CREATE SEQUENCE AM_MONETIZATION_PLATFORM_CUSTOMERS START WITH 1 INCREMENT BY 1;
CREATE TABLE IF NOT EXISTS AM_POLICY_SUBSCRIPTION (
POLICY_ID INTEGER DEFAULT NEXTVAL('AM_MONETIZATION_PLATFORM_CUSTOMERS'),
ID INTEGER NOT NULL AUTO_INCREMENT,
SUBSCRIBER_ID INTEGER NOT NULL,
TENANT_ID INTEGER NOT NULL,
CUSTOMER_ID VARCHAR(256) NOT NULL,
PRIMARY KEY (ID),
FOREIGN KEY (SUBSCRIBER_ID) REFERENCES AM_SUBSCRIBER(SUBSCRIBER_ID) ON DELETE CASCADE
);
CREATE SEQUENCE AM_MONETIZATION_SHARED_CUSTOMERS START WITH 1 INCREMENT BY 1;
CREATE TABLE IF NOT EXISTS AM_POLICY_SUBSCRIPTION (
POLICY_ID INTEGER DEFAULT NEXTVAL('AM_MONETIZATION_SHARED_CUSTOMERS'),
ID INTEGER NOT NULL AUTO_INCREMENT,
APPLICATION_ID INTEGER NOT NULL,
API_PROVIDER VARCHAR(256) NOT NULL,
TENANT_ID INTEGER NOT NULL,
SHARED_CUSTOMER_ID VARCHAR(256) NOT NULL,
PARENT_CUSTOMER_ID INTEGER NOT NULL,
PRIMARY KEY (ID),
FOREIGN KEY (APPLICATION_ID) REFERENCES AM_APPLICATION(APPLICATION_ID) ON DELETE CASCADE,
FOREIGN KEY (PARENT_CUSTOMER_ID) REFERENCES AM_MONETIZATION_PLATFORM_CUSTOMERS(ID) ON DELETE CASCADE
);
CREATE SEQUENCE AM_MONETIZATION_SUBSCRIPTIONS START WITH 1 INCREMENT BY 1;
CREATE TABLE IF NOT EXISTS AM_POLICY_SUBSCRIPTION (
POLICY_ID INTEGER DEFAULT NEXTVAL('AM_MONETIZATION_SUBSCRIPTIONS'),
ID INTEGER NOT NULL AUTO_INCREMENT,
SUBSCRIBED_APPLICATION_ID INTEGER NOT NULL,
SUBSCRIBED_API_ID INTEGER NOT NULL,
TENANT_ID INTEGER NOT NULL,
SUBSCRIPTION_ID VARCHAR(256) NOT NULL,
SHARED_CUSTOMER_ID INTEGER NOT NULL,
PRIMARY KEY (ID),
FOREIGN KEY (SUBSCRIBED_APPLICATION_ID) REFERENCES AM_APPLICATION(APPLICATION_ID) ON DELETE CASCADE,
FOREIGN KEY (SUBSCRIBED_API_ID) REFERENCES AM_API(API_ID) ON DELETE CASCADE,
FOREIGN KEY (SHARED_CUSTOMER_ID) REFERENCES AM_MONETIZATION_SHARED_CUSTOMERS(ID) ON DELETE CASCADE
);
I changed the table names based on what is used in MySQL script and created a Monetization policy. While I try to subscribe an API to the monetized policy, I am getting the following error:
ERROR - StripeMonetizationDAO Failed to add Stripe platform customer details for Subscriber : 1
ERROR - APIConsumerImpl Could not execute Workflow
org.wso2.carbon.apimgt.impl.workflow.WorkflowException: Error when inserting stripe customer details of username to Database
Caused by: org.postgresql.util.PSQLException: ERROR: null value in column "id" violates not-null constraint
Detail: Failing row contains (3, null, 1, -1234, cus_M7MKkaasfCbdf342SEn).
ERROR - GlobalThrowableMapper Could not execute Workflow
I am not sure if the above errors are due to the table name changes in the script or due to the update in workflow executors. I have added the following in workflow executors:
<SubscriptionCreation executor="<billing-engine-related-SubscriptionCreationWorkflowExecutor>"/>
<SubscriptionDeletion executor="<billing-engine-related-StripeSubscriptionDeletionWorkflowExecutor>"/>

There seems to be several issues with the documented db scripts for postgress and seems like we have missed it. Can you try the following scripts. Please note that i haven't tried it in postgress, but fixed multiple schema issues and this should work.
CREATE TABLE IF NOT EXISTS AM_MONETIZATION (
API_ID INTEGER NOT NULL,
TIER_NAME VARCHAR(512),
STRIPE_PRODUCT_ID VARCHAR(512),
STRIPE_PLAN_ID VARCHAR(512),
FOREIGN KEY (API_ID) REFERENCES AM_API (API_ID) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS AM_POLICY_PLAN_MAPPING (
POLICY_UUID VARCHAR(256),
PRODUCT_ID VARCHAR(512),
PLAN_ID VARCHAR(512),
FOREIGN KEY (POLICY_UUID) REFERENCES AM_POLICY_SUBSCRIPTION(UUID)
);
CREATE SEQUENCE AM_MONETIZATION_PLATFORM_CUSTOMERS_SEQ START WITH 1 INCREMENT BY 1;
CREATE TABLE IF NOT EXISTS AM_MONETIZATION_PLATFORM_CUSTOMERS (
ID INTEGER DEFAULT NEXTVAL('AM_MONETIZATION_PLATFORM_CUSTOMERS_SEQ'),
SUBSCRIBER_ID INTEGER NOT NULL,
TENANT_ID INTEGER NOT NULL,
CUSTOMER_ID VARCHAR(256) NOT NULL,
PRIMARY KEY (ID),
FOREIGN KEY (SUBSCRIBER_ID) REFERENCES AM_SUBSCRIBER(SUBSCRIBER_ID) ON DELETE CASCADE
);
CREATE SEQUENCE AM_MONETIZATION_SHARED_CUSTOMERS_SEQ START WITH 1 INCREMENT BY 1;
CREATE TABLE IF NOT EXISTS AM_MONETIZATION_SHARED_CUSTOMERS (
ID INTEGER DEFAULT NEXTVAL('AM_MONETIZATION_SHARED_CUSTOMERS_SEQ'),
APPLICATION_ID INTEGER NOT NULL,
API_PROVIDER VARCHAR(256) NOT NULL,
TENANT_ID INTEGER NOT NULL,
SHARED_CUSTOMER_ID VARCHAR(256) NOT NULL,
PARENT_CUSTOMER_ID INTEGER NOT NULL,
PRIMARY KEY (ID),
FOREIGN KEY (APPLICATION_ID) REFERENCES AM_APPLICATION(APPLICATION_ID) ON DELETE CASCADE,
FOREIGN KEY (PARENT_CUSTOMER_ID) REFERENCES AM_MONETIZATION_PLATFORM_CUSTOMERS(ID) ON DELETE CASCADE
);
CREATE SEQUENCE AM_MONETIZATION_SUBSCRIPTIONS_SEQ START WITH 1 INCREMENT BY 1;
CREATE TABLE IF NOT EXISTS AM_MONETIZATION_SUBSCRIPTIONS (
ID INTEGER DEFAULT NEXTVAL('AM_MONETIZATION_SUBSCRIPTIONS_SEQ'),
SUBSCRIBED_APPLICATION_ID INTEGER NOT NULL,
SUBSCRIBED_API_ID INTEGER NOT NULL,
TENANT_ID INTEGER NOT NULL,
SUBSCRIPTION_ID VARCHAR(256) NOT NULL,
SHARED_CUSTOMER_ID INTEGER NOT NULL,
PRIMARY KEY (ID),
FOREIGN KEY (SUBSCRIBED_APPLICATION_ID) REFERENCES AM_APPLICATION(APPLICATION_ID) ON DELETE CASCADE,
FOREIGN KEY (SUBSCRIBED_API_ID) REFERENCES AM_API(API_ID) ON DELETE CASCADE,
FOREIGN KEY (SHARED_CUSTOMER_ID) REFERENCES AM_MONETIZATION_SHARED_CUSTOMERS(ID) ON DELETE CASCADE
);

Related

mariadb won't create foreign key

I think I'm using the right syntax for MariaDB, but my foreign key constraint is not being created.
Here's the create table DDL:
CREATE TABLE items (
id INT auto_increment primary key,
description TEXT NOT NULL
);
CREATE TABLE item_events (
id INT NOT NULL,
calendar_event_guid TEXT(255) NOT NULL,
foreign key item_events_id_fk (id) REFERENCES items (id)
);
Then, when I ask MariaDB to show me what I created, I get this:
+-------------+-----------------
| Table | Create Table +-------------+-----------------
| item_events | CREATE TABLE `item_events` (
`id` int(11) NOT NULL,
`calendar_event_guid` text COLLATE utf8_unicode_ci NOT NULL,
KEY `item_events_id_fk` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
+-------------+-----------------
or, just showing the DDL:
CREATE TABLE `item_events` (
`id` int(11) NOT NULL,
`calendar_event_guid` text COLLATE utf8_unicode_ci NOT NULL,
KEY `item_events_id_fk` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
Notice that it only created a "KEY", not a foreign key. The items table is correctly created.
Surely, this is really simple :)

Why MySQL workbench is making all my foreign keys unique?

I've created a MySQL Model with a few tables, some of them with fk's to another table. I usually export the SQL from MySQL Model to my database using the "Forward Engineer SQL CREATE Script" inside File -> Export -> Forward Engineer SQL CREATE Script. The problem here is that when I generate the creation script, all my fk's become unique. I didn't check UQ option in MySQL Model but it creates a script with unique fk's anyway, so, I need to change the SQL file generated and remove all the unwanted uniques. Anyone has a clue why this is happening?
Generated script:
CREATE TABLE IF NOT EXISTS `u514786799_detranleiloes`.`Lotes` (
`createdAt` DATE NOT NULL,
`updatedAt` DATE NOT NULL,
`id` INT UNIQUE NOT NULL AUTO_INCREMENT,
`LeiloesId` INT UNIQUE NOT NULL,
`conservado` TINYINT NULL,
`numero` INT NOT NULL,
`CRDsId` INT UNIQUE NULL,
PRIMARY KEY (`id`),
INDEX `fk_Lotes_Leiloes_idx` (`LeiloesId` ASC),
INDEX `fk_Lotes_CRDs1_idx` (`CRDsId` ASC),
CONSTRAINT `fk_Lotes_Leiloes`
FOREIGN KEY (`LeiloesId`)
REFERENCES `u514786799_detranleiloes`.`Leiloes` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Lotes_CRDs1`
FOREIGN KEY (`CRDsId`)
REFERENCES `u514786799_detranleiloes`.`CRDs` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
AUTO_INCREMENT = 1;

How to make foreign key by two fields one of which is PK in H2?

I would like to define the following relation in H2:
So that relation connects two elements, but taking their types into acount: each relation type can connect two elements only of given element type.
Unfortunately, I can't draw FK from Relation to Element by two fields, one of fields is automatically dropped only one field remains
Also I can't define additional key in Elements table with two fields.
Is this possible in H2?
UPDATE
The same is in HSQLDB, but with error message.
Target Element table is
CREATE TABLE ELEM
(
ID INTEGER PRIMARY KEY NOT NULL IDENTITY,
TYP_ID INTEGER NOT NULL,
SPELL VARCHAR(12) NOT NULL,
CONSTRAINT ELEM_TYP_FK FOREIGN KEY (TYP_ID) REFERENCES TYP (ID) ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE UNIQUE INDEX "ELEM_ID_TYP_ID_uindex" ON ELEM (ID, TYP_ID);
CREATE UNIQUE INDEX "ELEM_SPELL_uindex" ON ELEM (SPELL);
As you see, there is an UNIQUE with (ID, TYP_ID)
Current relation table is
CREATE TABLE REL
(
LF_ID INTEGER NOT NULL,
LF_TYP_ID INTEGER NOT NULL,
RT_ID INTEGER NOT NULL,
RT_TYP_ID INTEGER NOT NULL,
TYP_ID INTEGER NOT NULL,
CONSTRAINT REL_LF_ID_LF_TYP_ID_RT_ID_RT_TYP_ID_PK PRIMARY KEY (LF_ID, LF_TYP_ID, RT_ID, RT_TYP_ID)
);
And command to try to relate:
ALTER TABLE REL ADD CONSTRAINT REL_LF_ELEM_TYP_FK FOREIGN KEY (LF_ID, LF_TYP_ID) REFERENCES ELEM (ID, TYP_ID);
Error message just lies:
UNIQUE constraint does not exist on referenced columns: ELEM in
statement [ALTER TABLE REL ADD CONSTRAINT REL_LF_ELEM_TYP_FK FOREIGN
KEY (LF_ID, LF_TYP_ID) REFERENCES ELEM (ID, TYP_ID)]

How to update and set unit_id to be NULL, unit id is foreign key from unit table

How to update and set unit_id to be NULL, unit id is foreign key and I need to set to be NULL
CREATE TABLE troops
(
id serial NOT NULL,
unit_id integer,
type integer NOT NULL,
level integer NOT NULL,
CONSTRAINT troops_pkey PRIMARY KEY (id),
CONSTRAINT troops_unit_id_fkey FOREIGN KEY (unit_id)
REFERENCES units (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE CASCADE
)
WITH (
OIDS=FALSE
);
I have tried to update troops and set unit_id to be NULL ( I put NULL not 0 in pqxx statement in C++ ) but I get error like
insert or update on table "troops" violates foreign key constraint "troops_unit_id_fkey"
DETAIL: Key (unit_id)=(0) is not present in table "units".
When I try from pgadmin I can set unit_id to be NULL in troops but pqxx ( from c++ code I try to update) converts NULL to 0, how to solve this ?

One to many mapping in Zend Framwork 2 with doctrine

I am trying to make a page where i handle my invoces. I have the invoice data in one tables and the invoice rows in another table. The tables looks as follows:
CREATE TABLE IF NOT EXISTS `Invoices` (
`I_Id` int(10) NOT NULL AUTO_INCREMENT,
`I_Number` int(4) NOT NULL,
`I_ClientId` int(10) NOT NULL,
`I_ExtraText` text NOT NULL,
PRIMARY KEY (`I_Id`)
) ENGINE=InnoDB
CREATE TABLE IF NOT EXISTS `InvoiceRows` (
`IR_Id` int(10) NOT NULL AUTO_INCREMENT,
`IR_InvoiceId` int(10) NOT NULL,
`IR_Price` int(10) NOT NULL,
`IR_Vat` smallint(2) unsigned NOT NULL,
`IR_Quantity` int(10) NOT NULL,
`IR_Text` varchar(255) NOT NULL,
PRIMARY KEY (`IR_Id`),
KEY `IR_InvoiceId` (`IR_InvoiceId`)
) ENGINE=InnoDB
Here is my mapping:
class Invoice {
/**
* #ORM\OneToMany(targetEntity="Row", mappedBy="invoice" ,cascade={"persist"})
*/
protected $rows;
}
class Row {
/**
* #ORM\ManyToOne(targetEntity="Invoice", inversedBy="rows", cascade={"persist"})
* #ORM\JoinColumn(name="IR_InvoiceId", referencedColumnName="I_Id")
**/
private $invoice;
}
I have been trying to follow the example at the doctrine docs on how to setup a One-To-Many, Bidirectional mapping. This is then connect with Zend Framework 2 and form collections. Pulling data works very good. I get all the rows of each invoice.
My Problem is when i want to write back to the database and save my changes. When i try to save i get the following error:
An exception occurred while executing 'INSERT INTO
MVIT_ADM__InvoiceRows (IR_InvoiceId, IR_Price, IR_Vat, IR_Quantity,
IR_Text) VALUES (?, ?, ?, ?, ?)' with params
{"1":null,"2":320,"3":0,"4":1,"5":"Learning your dog to sit"}:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column
'IR_InvoiceId' cannot be null
What have i done wrong? When checking the data from the post value is not empty.
Edit: Full source can be found at Github
It seems IR_InvoiceId null, it expect the Id of Invoices (I_Id) value, so make sure while you are inserting the data in InvoiceRows table then here pass the Invoices (I_Id) value as IR_InvoiceId as you mention table relation..
Best Of Luck!
Saran