Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

191
Views
Sqlite Foreign key mismatch sequelize migration

My request SQL fail when I try to insert data in my table PostVotes I use sequelize migration to migrate my database and I have the same probleme when I make my sql request in my console.

sequelize db:migrate

When I use sequelize.sync() i don't have this problem

My table:

/* TABLE Users */
  queryInterface.createTable('Users', {
    id: {
      allowNull: false,
      primaryKey: true,
      type: Sequelize.UUID,
      defaultValue: Sequelize.UUIDV4,
    },
    password: {
      type: Sequelize.STRING,
      allowNull: false,
    },
    email: {
      type: Sequelize.STRING,
      allowNull: false,
      unique: true,
    },
  }),
  queryInterface.createTable('Posts', {
    id: {
      allowNull: false,
      primaryKey: true,
      type: Sequelize.UUID,
      defaultValue: Sequelize.UUIDV4,
    },
    title: {
      type: Sequelize.STRING,
      allowNull: false,
    },
    userId: {
      type: Sequelize.UUID,
      primaryKey: true,
      onDelete: 'CASCADE',
      references: {
        model: 'Users',
        key: 'id',
      },
    },
  }),

  /* Table posts votes*/
  queryInterface.createTable('PostVotes', {
    id: {
      allowNull: false,
      primaryKey: true,
      type: Sequelize.UUID,
      defaultValue: Sequelize.UUIDV4,
    },
    vote: {
      // eslint-disable-next-line new-cap
      type: Sequelize.ENUM('up', 'down'),
      validate: {
        isIn: [['up', 'down']],
      },
      allowNull: false,
    },
    userId: {
      type: Sequelize.UUID,
      primaryKey: true,
      onDelete: 'CASCADE',
      references: {
        model: 'Users',
        key: 'id',
      },
    },
    postId: {
      type: Sequelize.UUID,
      primaryKey: true,
      onDelete: 'CASCADE',
      references: {
        model: 'Posts',
        key: 'id',
      },
    },
  }),

[Error: SQLITE_ERROR: foreign key mismatch - "PostVotes" referencing "Posts"]

I already find answer but nothing is working. I think my mistake is in the table PostVotes but I don't understand what.

Thanks !

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

FIX

queryInterface.createTable('Posts', {
id: {
  allowNull: false,
  primaryKey: true,
  type: Sequelize.UUID,
  defaultValue: Sequelize.UUIDV4,
},
title: {
  type: Sequelize.STRING,
  allowNull: false,
},
userId: {
  type: Sequelize.UUID,
  primaryKey: true, # DELETE THIS ONE
  onDelete: 'CASCADE',
  references: {
    model: 'Users',
    key: 'id',
  },
},

}),

I delete primaryKey in table Post column userId

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!