Skip to content

CreateTable() silently fails when specifying default charset #853

@dalers

Description

@dalers

Help please :-)

I created a new table in a db update script using the following snippet:

// 4. ADD TABLE "stockfils" STOCK ITEM RELATED FILE/URL TABLE
//    - for file name with path OR URL
//    - equivalent to P&V FIL table
//    - Parts&Vendors allows ?? char TODO confirm max number of characters
//    - has foreign key constraint to stockmaster.stockid

// CreateTable($Table, $SQL)
CreateTable('stockfils', 'CREATE TABLE `stockfils` (
  `filid` INTEGER NOT NULL AUTO_INCREMENT, 
  `filstockid` VARCHAR(64),
  `filfilepath` VARCHAR(255), 
  `filefilename` VARCHAR(255), 
  `filview` TINYINT(1) DEFAULT 0, 
  `filnotes` VARCHAR(50), 
  INDEX (`filid`, `filstockid`), 
  INDEX (`filstockid`), 
  PRIMARY KEY (`filid`)
)');

and the table is created as expected:

Image

I then tried creating a foreign key constraint from stockfils.filstockid to stockmaster.stockid:

// AddConstraint($Table, $Constraint, $Field, $ReferenceTable, $ReferenceField)
AddConstraint('stockfils', 'stockfils_ibfk_1', 'filstockid', 'stockmaster', 'stockid');

but when Z_UpgradeDatabase.php runs it reports:

Warning: mysqli_query(): (HY000/1822): Failed to add the foreign key constraint. Missing index for constraint 'stockfils_ibfk_1' in the referenced table 'stockmaster' in C:\xampp\htdocs\weberp\includes\ConnectDB_mariadb.php on line 63

The constraint stockfils_ibfk_1 could not be added
ALTER TABLE stockfils ADD CONSTRAINT stockfils_ibfk_1 FOREIGN KEY (filstockid) REFERENCES stockmaster (stockid)
Image

Attempting to execute the equivalent (IIUC) SQL query:

ALTER TABLE stockfilsADD CONSTRAINTstockfils_ibfk_1 FOREIGN KEY (filstockid) REFERENCES stockmaster(stockid) ON DELETE RESTRICT ON UPDATE RESTRICT;

directly in phpMyAdmin results in the error:

Error creating foreign key on filstockid (check data types)

I'm confused because both stockfils.filestockid and stockmaster.stockid are VARCHAR(64). Can anyone point me in the right direction to resolve this?

Fwiw, I noticed the collation for stockfils.filstockid is utf8_general_ci (see above screenshot), but the collation for stockmaster.stockid is utf8mb4_general_ci.

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions