gpt4 book ai didi

sequelize.js - 如何获得 sequelize 以创建连接表的 id?

转载 作者:行者123 更新时间:2023-12-03 22:16:09 24 4
gpt4 key购买 nike

查看这些模型和关系:

var User = sequelize.define('user', {
name: {type: Sequelize.STRING, unique: true},
password: Sequelize.STRING,
email: Sequelize.STRING
});

var Group = sequelize.define('group', {
name: Sequelize.STRING,
});

var Membership = sequelize.define('membership', {
foo: Sequelize.STRING
});

var Query = sequelize.define('query', {
text: Sequelize.STRING,
});

User.belongsToMany(Group, {through: Membership});
Group.belongsToMany(User, {through: Membership});

Query.belongsTo(Membership);
Membership.hasMany(Query);

为什么 sequelize 不为 Membership 创建一个 id 列?我怎样才能让它创建它?为什么它会创建一个名为 membershipGroupId 的列?

要回答@denisazevedo,我将 syncforce 一起使用。下面是同步的输出
Executing (default): DROP TABLE IF EXISTS `memberships`;
Executing (default): DROP TABLE IF EXISTS `groups`;
Executing (default): DROP TABLE IF EXISTS `cookies`;
Executing (default): DROP TABLE IF EXISTS `events`;
Executing (default): DROP TABLE IF EXISTS `clients`;
Executing (default): DROP TABLE IF EXISTS `users`;
Executing (default): DROP TABLE IF EXISTS `results`;
Executing (default): DROP TABLE IF EXISTS `queries`;
Executing (default): DROP TABLE IF EXISTS `queries`;
Executing (default): CREATE TABLE IF NOT EXISTS `queries` (`id` INTEGER NOT NULL auto_increment , `text` VARCHAR(255), `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, `membershipGroupId` INTEGER, PRIMARY KEY (`id`)) ENGINE=InnoDB;
Executing (default): SHOW INDEX FROM `queries`
Executing (default): DROP TABLE IF EXISTS `results`;
Executing (default): CREATE TABLE IF NOT EXISTS `results` (`id` INTEGER NOT NULL auto_increment , `link` VARCHAR(2048), `description` TEXT, `result_order` FLOAT(5,2), `title` VARCHAR(255), `result_relevance` ENUM('up', 'down', 'none'), `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, `queryId` INTEGER, PRIMARY KEY (`id`), FOREIGN KEY (`queryId`) REFERENCES `queries` (`id`) ON DELETE SET NULL ON UPDATE CASCADE) ENGINE=InnoDB;
Executing (default): SHOW INDEX FROM `results`
Executing (default): DROP TABLE IF EXISTS `users`;
Executing (default): CREATE TABLE IF NOT EXISTS `users` (`id` INTEGER NOT NULL auto_increment , `name` VARCHAR(255) UNIQUE, `password` VARCHAR(255), `email` VARCHAR(255), `role` ENUM('facilitator', 'participant'), `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB;
Executing (default): SHOW INDEX FROM `users`
Executing (default): DROP TABLE IF EXISTS `clients`;
Executing (default): CREATE TABLE IF NOT EXISTS `clients` (`id` INTEGER NOT NULL auto_increment , `socketid` VARCHAR(255), `connected` DATETIME, `disconnected` DATETIME, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, `userId` INTEGER, PRIMARY KEY (`id`), FOREIGN KEY (`userId`) REFERENCES `users` (`id`) ON DELETE SET NULL ON UPDATE CASCADE) ENGINE=InnoDB;
Executing (default): SHOW INDEX FROM `clients`
Executing (default): DROP TABLE IF EXISTS `events`;
Executing (default): CREATE TABLE IF NOT EXISTS `events` (`id` INTEGER NOT NULL auto_increment , `description` TEXT, `type` ENUM('vote_up', 'vote_down', 'critisort', 'originalsort', 'logout', 'login', 'follow', 'search'), `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, `clientId` INTEGER, `resultId` INTEGER, `queryId` INTEGER, PRIMARY KEY (`id`), FOREIGN KEY (`clientId`) REFERENCES `clients` (`id`) ON DELETE SET NULL ON UPDATE CASCADE, FOREIGN KEY (`resultId`) REFERENCES `results` (`id`) ON DELETE SET NULL ON UPDATE CASCADE, FOREIGN KEY (`queryId`) REFERENCES `queries` (`id`) ON DELETE SET NULL ON UPDATE CASCADE) ENGINE=InnoDB;
Executing (default): SHOW INDEX FROM `events`
Executing (default): DROP TABLE IF EXISTS `cookies`;
Executing (default): CREATE TABLE IF NOT EXISTS `cookies` (`id` INTEGER NOT NULL auto_increment , `key` VARCHAR(255), `uid` VARCHAR(255) UNIQUE, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB;
Executing (default): SHOW INDEX FROM `cookies`
Executing (default): DROP TABLE IF EXISTS `groups`;
Executing (default): CREATE TABLE IF NOT EXISTS `groups` (`id` INTEGER NOT NULL auto_increment , `name` VARCHAR(255), `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, `ownerId` INTEGER, PRIMARY KEY (`id`), FOREIGN KEY (`ownerId`) REFERENCES `users` (`id`) ON DELETE SET NULL ON UPDATE CASCADE) ENGINE=InnoDB;
Executing (default): SHOW INDEX FROM `groups`
Executing (default): DROP TABLE IF EXISTS `memberships`;
Executing (default): CREATE TABLE IF NOT EXISTS `memberships` (`foo` VARCHAR(255), `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, `groupId` INTEGER , `userId` INTEGER , PRIMARY KEY (`groupId`, `userId`), FOREIGN KEY (`groupId`) REFERENCES `groups` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, FOREIGN KEY (`userId`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE) ENGINE=InnoDB;
Executing (default): SHOW INDEX FROM `memberships`

最佳答案

正如您在 SQL 中看到的那样,Membership 中的外键是按预期创建的:

  • groupId
  • userId

  • 要使 Membership 拥有自己的 ID,您需要按照 Belongs-To-Many documentation 中的详细说明手动将其添加到表中:

    By default the code above will add projectId and userId to the UserProjects table, and remove any previously defined primary key attribute - the table will be uniquely identified by the combination of the keys of the two tables, and there is no reason to have other PK columns. To enforce a primary key on the UserProjects model you can add it manually.

    UserProjects = sequelize.define('userProjects', {
    id: {
    type: Sequelize.INTEGER,
    primaryKey: true,
    autoIncrement: true
    },
    status: DataTypes.STRING
    })


    我建议阅读整个 section,因为它包含有用的信息。

    关于sequelize.js - 如何获得 sequelize 以创建连接表的 id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36898526/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com