Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6140319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:06:09+00:00 2026-05-23T18:06:09+00:00

I have two tables: invoices & invoice_items. I need a one to many relationship

  • 0

I have two tables: invoices & invoice_items. I need a one to many relationship between the two with cascading delete (so if the invoice is deleted, the items also get deleted).

The primary key on invoices spans two columns: invoice_number, vendor_number

The primary key on invoice_items spans three columns: invoice_number, vendor_number, item_number

How do I add a foreign key constraint to the invoice_items table using the invoice_number & vendor_number columns?

I tried this and it didn’t work:

ALTER TABLE `invoice_items`
ADD FOREIGN KEY (`invoice_number`,`vendor_number`)
REFERENCES `invoices`(`invoice_number`,`vendor_number`) ON DELETE CASCADE;

ERROR 1005 (HY000): Can't create table 'test_db.#sql-12c8_db1ad' (errno: 150)

Here are the table definitions:

CREATE TABLE IF NOT EXISTS `invoices` (
  `vendor_number` varchar(20) NOT NULL,
  `invoice_number` varchar(20) NOT NULL,
  `po_number` varchar(50) NOT NULL,
  `inbound_message_id` int(11) NOT NULL,
  `created_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`vendor_number`,`invoice_number`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS `invoice_items` (
  `vendor_number` varchar(20) NOT NULL,
  `invoice_number` varchar(20) NOT NULL,
  `po_item_number` varchar(6) NOT NULL,
  `quantity` float NOT NULL,
  `amount` decimal(10,2) NOT NULL COMMENT 'Total amount invoiced for this line item',
  PRIMARY KEY (`vendor_number`,`invoice_number`,`po_item_number`),
  KEY `invoice_key` (`vendor_number`,`invoice_number`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Here is the output from show innodb status:

110707 12:26:19 Error in foreign key constraint of table test_db/#sql-12c8_dcbfb:
FOREIGN KEY (`invoice_number`,`vendor_number`)
REFERENCES `invoices`(`invoice_number`,`vendor_number`) ON DELETE CASCADE:
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
See http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html
for correct foreign key definition.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T18:06:10+00:00Added an answer on May 23, 2026 at 6:06 pm

    Use show innodb status. This will dump out a large block of text. In the middle somewhere is a “last foreign key error”. It’ll contain more details as to why the alter table failed.

    One possibility is a mismatch in field types. The keyed fields must be exact duplicates in both tables. You can’t link a signed field to an unsigned one, or a int to a bigint, etc…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables: invoice and charges, with a one-to-many relationship. (simplified) invoice has:
So I have two tables, Invoices and InvoiceItems. When I delete an Invoice, I'd
I have two tables (Reservation) and (Invoice). Reservation incurs one invoice, an invoice is
I need to map one entity to two tables (Invoice and InvoiceHistory). It's not
I have two tables, one for invoices and one for incoming payments. An incoming
I have two tables invoices and pending_payments both of which have the following rows
I have two tables one with ID and NAME table 1 ID | NAME
I have two tables in my MySQL database, one is a library of all
I have two tables: Invoice (ID, InternalPrice, ExternalPrice) InvoiceSummary (InvoiceID, Variance ) In this
I have two tables in MySql database, one is sales_order and the other is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.