Search code examples
mysqlsqlforeign-keysmysql-error-1064

MySQL error:1251 cannot add foreign key


The first 4 table are created fine, the transactions tables run into problem. I get the 1215 error: cannot add foreign key. I've checked an rechecked the data types, and made sure all FK are PK of their own tables. What's wrong here?

CREATE SCHEMA FinalDB;

CREATE TABLE `User` (
    userId int not null auto_increment primary key,
    first_name varchar(255) not null,
    last_name varchar(255) not null,
    address varchar(255) null,
    DOB date not null,
    availableBalance int not null default 0,
    currency varchar(20)
);

CREATE TABLE Verifications(
    userId int not null primary key,
    passport int null,
    ssn int null,
    license int null,
    constraint
    foreign key (userId)
    references User(userId)
);

CREATE TABLE Linked_Account(
    account_Id int not null, 
    userId int not null,
    routing int null,
    swift int null,
    primary key (userId, account_Id),
    constraint
    foreign key (userId) 
    references User(userId)
);

CREATE TABLE Wallet (
    userId int not null,
    walletId varchar(5) not null,
    coinAmount int not null default 0,
    netWorth int not null default 0,
    primary key(userId, walletId),
    constraint
    foreign key (userId)
    references `User`(userId)
);

CREATE TABLE Transactions (
    transactionId int not null primary key auto_increment, 
    userId int not null,
    type varchar(30) not null,
    walletId varchar(5) not null,
    payment_method int null, #optional
    total int null, #optional
    quantity int not null,
    fee int null, #optional
    `date` date not null,
    sender varchar(50) null, #optional
    reciever varchar(50) null, #optional
    status varchar(20) not null,
    notes varchar(200) null, #optional
    constraint 
    foreign key (userId)
    references `User`(userId) 
    ON DELETE CASCADE ON UPDATE CASCADE,
    constraint 
    foreign key (walletId)
    references Wallet(walletId)
    ON DELETE CASCADE ON UPDATE CASCADE,
    constraint
    foreign key (payment_method)
    references Linked_Account(account_id)

);

CREATE TABLE TransactionsExchange(
    transactionId int not null auto_increment primary key,
    userId int not null,
    currencyFrom int not null,
    currencyFromAmount int not null,
    currencyInto int not null,
    currencyIntoEquivalent int not null,
    notes varchar(200) null,
    `date` date not null,
    constraint
    foreign key (userId)
    references User(userId),
    constraint
    foreign key (currencyFrom)
    references Wallet(walletId),
    constraint
    foreign key (currencyInto)
    references Wallet(walletId)
);

I've look online for possible answer, but it's usually having to do with inconsistent data types or undeclared PK's. I'm basically trying to make a transactions table to log various different data in different compositions. Using backend logic to handle what is required and what is not, aside from a few defaults.


Solution

  • To use a compound Primary Key as Foreign Key, you'll have to add the same number of columns (that compose the PK) with same datatypes to the child table and then use the combination of these columns in the FOREIGN KEY definition.

    see related post here https://stackoverflow.com/a/10566463/4904726

    Try this 'Transactions' table creating query:

     CREATE TABLE Transactions (
                transactionId int not null primary key auto_increment, 
                userId int not null,
                type varchar(30) not null,
                walletId varchar(5) not null,
                payment_method int null, #optional
                total int null, #optional
                quantity int not null,
                fee int null, #optional
                `date` date not null,
                sender varchar(50) null, #optional
                reciever varchar(50) null, #optional
                status varchar(20) not null,
                notes varchar(200) null, #optional
                constraint 
                foreign key (userId)
                references `User`(userId) 
                ON DELETE CASCADE ON UPDATE CASCADE,
                constraint
                foreign key (userId, walletId)
                references Wallet(userId, walletId)
                ON DELETE CASCADE ON UPDATE CASCADE,
                constraint
                foreign key (userId, payment_method)
                references Linked_Account(userId, account_id)
    
            );