- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在处理 mysql 数据库时,我正在使用sequelize-cli 迁移。
如果我输入命令
sequelize init
创建了两个文件夹models
和migrations
此外,例如,如果我在两个模型之间设置关联
User.hasMany(Posts)
我必须手动将外键添加到迁移文件中。
(但不是模型文件,对吧?)
此外,迁移文件中还有诸如created_at、updated_at之类的列
但不在模型文件中。
当使用db.sync();
时,没有这样的迁移文件,因此我不必手动为模型文件添加外键。
我知道迁移文件仅与数据库表架构有关,但是模型文件实际上在做什么......?
该模型文件与数据库表无关吗?
我了解同步和迁移之间的区别,(如果要设置force:true但不设置迁移,则同步删除所有表)
但也许我无法理解内部发生的事情(迁移之间的模型)
如有任何建议,我们将不胜感激!
最佳答案
当您使用sequelize-cli时,已经为您创建了createdAt和updatedAt列,因为sequelize使用它们来更新表。您可以禁用它们。
sequelize 模型文件不需要createdAt 和updatedAt 来定义更改,因为它们是由sequelize 完成的。
您必须手动将外键添加到迁移文件和模型中。模型文件是您的sequelize与数据库架构交互的引用。
它为您的 Sequelize 设置参数以触发对数据库的查询。您可以说迁移文件与您的数据库架构有关,而模型文件与您的数据库中存储的数据有关。
示例考虑用户表及其基本信息。
用户模型
module.exports = (sequelize, DataTypes) => {
var Users = sequelize.define('users', {
user_id: {
type: DataTypes.STRING,
primaryKey: true,
},
}, {});
User.associate = function(models) {
Users.hasOne(models.basicinfo, {
foreignKey: 'user_id',
as: 'BasicInfo'
});
};
return Users;
};
基本信息型号
module.exports = (sequelize, DataTypes) => {
var basicinfo = sequelize.define('basicinfo', {
first_name: {
type: DataTypes.STRING,
},
last_name: {
type: DataTypes.STRING,
},
};{});
basicinfo.associate = function (models) {
basicinfo.belongsTo(models.users, {
foreignKey: 'user_id',
onDelete: 'CASCADE'
});
};
return basicinfo;
};
希望这能消除您的困惑,当您使用这个伟大的库时,您会学到更多。
这是我更新的答案,其中包含每个模型的迁移文件。
用户迁移
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('users', {
user_id: {
type: Sequelize.STRING,
primaryKey: true,
},
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('users');
}
};
基本信息迁移
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('basicinfo', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
user_id: {
type: Sequelize.STRING,
allowNull: false,
unique: true,
onDelete: 'CASCADE',
references: {
model: 'users',
key: 'user_id',
as: 'user_id'
}
},
profile_img: {
type: Sequelize.STRING
},
first_name: {
type: Sequelize.STRING,
},
last_name: {
type: Sequelize.STRING,
},
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('basicinfo');
}
};
关于mysql - 表达与迁移文件相比,模型文件实际上在做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50444226/
渐进增强和优雅降级基本是一回事吗? 最佳答案 不完全是。他们从不同的 Angular 解决类似的问题。 “优雅的降级”意味着你有漂亮的功能,并且可以在不支持它的浏览器中处理它不那么漂亮(但仍然需要它以
在过去的几周里,我一直在调优和处理 PostgreSQL,我将在我的下一个项目中使用它。 我的规范是: DigitalOcean 8 核 16GB SSD x2(一个用于数据库,另一个用于 Web)
我看过很多关于负数模的问题的答案。每一个答案都放了标准 (a/b)*b + a%b is equal to a 解释。我可以用这种方法计算任何模数,而且我知道有必要使用一个模数函数,如果它是负数,则将
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 9 年前。 Improve
The docs假设所有标签都存储在 .hgtags 中,但这里显然存在一些黑魔法。 我的标签如下所示: mbayazit:~/test$ cat .hgtags 0d80b6ba4ba3b51a44
我正在尝试强制删除待处理的更改列表。所有文件(20 个旧文件)都是新文件,但尚未提交/提交。所以在 p4Win 中,它们显示红色 + 十字。我无法从更改列表中删除这些文件。我该如何删除这些文件? 感谢
如果我要删除的文件不属于工作区,那么如何从工作区的目录中删除文件? 我的文件系统上有一个目录,其中包含从 perforce 获取的文件,但在某些进程运行后,它会在这些目录中创建一些新文件。 是否有 p
就是好奇这个。以下是同一功能的两个代码片段: void MyFunc1() { int i = 10; object obj = null; if(something) ret
我对使用约束布局还很陌生,我在调整布局大小方面遇到了问题,我希望它能够响应,这样我就不必再为不同的屏幕尺寸制作 10 个布局。在布局编辑器中,一切在不同尺寸下看起来都很完美,但实际上并非如此。 我做了
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
如果试图修改声明为 const 的对象,const 类型限定符会导致编译器发出错误消息,但这还不够保护。例如以下程序修改声明为 const 的数组的两个元素: #include int main(v
我不得不问这个,因为:我唯一知道的是,如果断言失败,应用程序就会崩溃。这就是为什么要使用 NSAssert 的原因吗?或者这样做还有什么好处?将 NSAssert 置于我在代码中所做的任何假设之上是否
我正在处理我的操作系统项目的 POSIX 子系统,并且我已经达到了我想要处理 pthreads 支持的地步。但是,我不确定我应该在多大程度上实现它们。 最常用的 pthreads 功能是什么?现在有什
这个问题不太可能对任何 future 的访客有帮助;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于互联网的全局受众。如需帮助使这个问题更广泛适用,visit the h
我正在尝试运行测试类,但抛出错误实际上有零交互。 class Xtractor{ void extractValues(request,Map m1, Map m2,Map m3){
我有一个抽象类UIObject,如下所示: public abstract class UIObject { private final int tabOrder; public UI
这是我尝试在 emacs lisp 中进行一些计算时得到的... (+ 2082844800. 1274511600.0) => 1209872752.0 (+ 2082844800.0 127451
我想用一条垂直线将屏幕分成两部分。垂直线应该从屏幕底部一直延伸到导航栏。如果我们使用 html/css,我只会有 2 个 div,并在右侧 div 上放置一个左边框。如果有办法在 View 的单侧放置
我有一个EC2实例可以正常工作数月(仍在开发中,应用程序尚未启用),但是我只是意识到我什至不知道如何根据流量来扩大/缩小EC2实例。 亚马逊提供的大量服务是压倒性的,我对此感到非常困惑。 最初,虽然我
考虑这个代码: int i = 1; int x = ++i + ++i; 我们对编译器可能会为这段代码做些什么有一些猜测,假设它可以编译。 两者 ++i返回 2 ,导致 x=4 . 一 ++i返回
我是一名优秀的程序员,十分优秀!