title: Invalid Columns in referenced table
body: <p>Im setting up a couple tables in a new database. I have a <code>Users</code> table, a <code>Companies</code> table, a <code>UserCompanies</code> table (so a user can work for many different companies), and a <code>UserPay</code> Table. </p>
  
  <pre><code>CREATE TABLE tblUsers
  (
  [UserTableID] INT IDENTITY NOT NULL PRIMARY KEY,    
  [UserEmail] VARCHAR(50) NOT NULL,
  [UserFirstName] VARCHAR(20) NOT NULL,
  [UserLastName] VARCHAR(20) NOT NULL,
  [UserAddress1] VARCHAR(45) NULL,
  [UserAddress2] VARCHAR(45) SPARSE NULL,
  [UserCity] VARCHAR(45) NULL,
  [UserState] VARCHAR(2) NULL,
  [UserZipCode] VARCHAR(10) NULL,
  [UserPhone] VARCHAR(13) NULL,
  )
  
  CREATE TABLE tblCompanies
  (
  [CompanyID] INT IDENTITY NOT NULL PRIMARY KEY,
  [CompanyName] VARCHAR(45) NOT NULL,
  [CompanyAddress1] VARCHAR(45) NULL,
  [CompanyAddress2] VARCHAR(45) SPARSE NULL,
  [CompanyCity] VARCHAR(45) NULL,
  [CompanyState] VARCHAR(2) NULL,
  [CompanyPhone] VARCHAR(13) NULL,
  [CompanyPhoneExtension] VARCHAR(8) NULL,
  [CompanyContactFirstName] VARCHAR(20) NOT NULL,
  [CompanyContactLastName] VARCHAR(20) NOT NULL
  )
  
  CREATE TABLE tblUserCompanies
  (
  [UserTableID] INT NOT NULL,
  [CompanyID] INT NOT NULL,
  PRIMARY KEY (UserTableID, CompanyID),
  FOREIGN KEY (UserTableID) REFERENCES tblUsers(UserTableID),
  FOREIGN KEY (CompanyID) REFERENCES tblCompanies(CompanyID)  
  )
  
  CREATE TABLE tblUserPay
  (
  [UserPayID] INT IDENTITY NOT NULL PRIMARY KEY,
  [UserID] INT NOT NULL REFERENCES tblUserCompanies(UserTableID),
  [UserCompany] INT NOT NULL REFERENCES tblUserCompanies(CompanyID),
  [NetPay] MONEY NOT NULL,
  [PayDate] DATE NOT NULL
  )
  </code></pre>
  
  <p>The problem is I get an error which states.. "There are no primary or candidate keys in referenced table <code>tblUserCompanies</code> that match the referencing column list in <code>tblUserPay</code>.</p>
  
  <p>I dont understand why Im getting error. Same data type, foreign keys in tact, primary keys established. Only thing I can think of is that its throwing an error because i have a foreign key linking to another foreign key. Please help if you can! Thanks</p>