创建表时外键约束格式不正确

Foreign key constraint is incorrectly formed when creating table

提问人:tsacarm 提问时间:11/11/2023 最后编辑:Raymond Chentsacarm 更新时间:11/11/2023 访问量:23

问:

我是 nextjs 的新手,正在使用 next-auth 我遇到了一个疯狂的错误,我没有找到任何线索 希望你能帮到我 现在太绝望了 被困在这里好几天了 下面是我收到的错误

```Executing (default): CREATE TABLE IF NOT EXISTS `accounts` (`id` CHAR(36) BINARY , `type` VARCHAR(255) NOT NULL, `provider` VARCHAR(255) NOT NULL, `provider_account_id` VARCHAR(255) NOT NULL, `refresh_token` VARCHAR(255), `access_token` VARCHAR(255), `expires_at` INTEGER, `token_type` VARCHAR(255), `scope` VARCHAR(255), `id_token` TEXT, `session_state` VARCHAR(255), `user_id` CHAR(36) BINARY, PRIMARY KEY (`id`), FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE) ENGINE=InnoDB;
Unable to connect to the database: Error
    at Query.run (webpack-internal:///(rsc)/./node_modules/sequelize/lib/dialects/mysql/query.js:58:29)
    at eval (webpack-internal:///(rsc)/./node_modules/sequelize/lib/sequelize.js:321:36)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async MySQLQueryInterface.createTable (webpack-internal:///(rsc)/./node_modules/sequelize/lib/dialects/abstract/query-interface.js:101:16)
    at async account.sync (webpack-internal:///(rsc)/./node_modules/sequelize/lib/model.js:1012:13)
    at async Sequelize.sync (webpack-internal:///(rsc)/./node_modules/sequelize/lib/sequelize.js:386:17)
    at async eval (webpack-internal:///(rsc)/./database/sequelize.js:18:9) {
  name: 'SequelizeDatabaseError',
  parent: Error: Can't create table `tms`.`accounts` (errno: 150 "Foreign key constraint is incorrectly formed")
      at Packet.asError (webpack-internal:///(rsc)/./node_modules/mysql2/lib/packets/packet.js:647:21)
      at Query.execute (webpack-internal:///(rsc)/./node_modules/mysql2/lib/commands/command.js:25:32)
      at Connection.handlePacket (webpack-internal:///(rsc)/./node_modules/mysql2/lib/connection.js:427:40)
      at PacketParser.eval [as onPacket] (webpack-internal:///(rsc)/./node_modules/mysql2/lib/connection.js:83:18)
      at PacketParser.executeStart (webpack-internal:///(rsc)/./node_modules/mysql2/lib/packet_parser.js:75:26)
      at Socket.eval (webpack-internal:///(rsc)/./node_modules/mysql2/lib/connection.js:90:31)
      at Socket.emit (node:events:513:28)
      at addChunk (node:internal/streams/readable:324:12)
      at readableAddChunk (node:internal/streams/readable:297:9)
      at Readable.push (node:internal/streams/readable:234:10)
      at TCP.onStreamRead (node:internal/stream_base_commons:190:23) {
    code: 'ER_CANT_CREATE_TABLE',
    errno: 1005,
    sqlState: 'HY000',
    sqlMessage: 'Can\'t create table `tms`.`accounts` (errno: 150 "Foreign key constraint is incorrectly formed")',
    sql: 'CREATE TABLE IF NOT EXISTS `accounts` (`id` CHAR(36) BINARY , `type` VARCHAR(255) NOT NULL, `provider` VARCHAR(255) NOT NULL, `provider_account_id` VARCHAR(255) NOT NULL, `refresh_token` VARCHAR(255), `access_token` VARCHAR(255), `expires_at` INTEGER, `token_type` VARCHAR(255), `scope` VARCHAR(255), `id_token` TEXT, `session_state` VARCHAR(255), `user_id` CHAR(36) BINARY, PRIMARY KEY (`id`), FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE) ENGINE=InnoDB;',
    parameters: undefined
  },
  original: Error: Can't create table `tms`.`accounts` (errno: 150 "Foreign key constraint is incorrectly formed")

我的 NextJS 试图创建一个在我的数据库模型中不存在的表,称为“帐户” 当我查看它创建的帐户详细信息时 我看到了

provider` VARCHAR(255) NOT NULL, `provider_account_id` VARCHAR(255) NOT NULL, `refresh_token` VARCHAR(255),

我在某个地方配置错了 我的下一个身份验证?

import NextAuth from "next-auth/next"
import CredentialsProvider  from "next-auth/providers/credentials";
import SequelizeAdapter from "@auth/sequelize-adapter"
import sequelize  from  "../../../../database/sequelize"
import bcrypt from 'bcrypt'
import User from '../../../../database/models/user'

export const authOptions = ({
  adapter: SequelizeAdapter(sequelize),
  providers: [
    CredentialsProvider({
        name: "credentials",
        credentials:{
            email:{label: "email", type:"email", placeholder:"[email protected]" },
            password:{label:"password", type:"password"}
        },
        async authorize(credentials) {
          
          const { email, password } =  credentials
          console.log(credentials)
          const user = await User.findOne({ where: { email: email } });
          console.log("this is user" + user)
          if (!user) {
            throw new Error("Invalid username and password");
          }
        
          const isMatch = await bcrypt.compare(password, user.password);
          if (!isMatch) {
            throw new Error("Invalid username and password");
          }
          
          if(user && isMatch){
            return user
          }
        }
    })
  ],
  session: {
    strategy: "jwt",
    maxAge: 30 * 24 * 60 * 60, // 30 days
  },
  pages: {
    signIn: '/',

  },
  callbacks: {

    async jwt({token, user, account, profile, email, credentials}){
      console.log(token)
      console.log(user)
      console.log(account)
      console.log(profile)
      console.log(email)
      console.log(credentials)
      if(user){
        token = user.token
      }return token
    },
    async session({session, token}){
      if(token){
        session.token = token
      }
      return session
    },
  },
  secret: process.env.NEXTAUTH_SECRET
})

const handler = NextAuth(authOptions)
export { handler as GET, handler as POST }

下一个 .js 续集:.js 下一个身份验证

评论


答: 暂无答案