- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的图表数据库:/image/CGAwh.png
我用 SEQUELIZE 制作了我的数据库模型:
型号:级别
module.exports = (sequelize, DataTypes) => {
const Level = sequelize.define(
'Level',
{
level_id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
label: {
type: DataTypes.STRING,
allowNull: false,
unique: {
args: true,
msg: 'Level:Label already exist!',
},
validate: {
notEmpty: { msg: `Level:Label cannot be empty!` },
notNull: { msg: `Level:Label cannot be NULL!` },
},
},
ref: {
type: DataTypes.STRING,
allowNull: true,
},
description: {
type: DataTypes.TEXT,
allowNull: true,
},
},
{
tableName: 'levels',
timestamps: false,
}
);
Level.associate = (models) => {
Level.belongsToMany(models.Test, {
through: models.testHasLevel,
foreignKey: 'level_id',
otherKey: 'test_id',
timestamps: false,
});
};
return Level;
};
型号:测试:
module.exports = (sequelize, DataTypes) => {
const Test = sequelize.define(
'Test',
{
test_id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
label: {
type: DataTypes.STRING,
allowNull: false,
validate: {
notEmpty: { msg: `Test:label cannot be empty!` },
notNull: { msg: `Test:label cannot be NULL!` },
},
},
isInternal: {
type: DataTypes.BOOLEAN,
defaultValue: false,
allowNull: false,
validate: {
notEmpty: { msg: `Test:isInternal cannot be empty!` },
notNull: { msg: `Test:isInternal cannot be NULL!` },
},
},
parent_id: {
type: DataTypes.INTEGER,
defaultValue: null,
allowNull: true,
},
},
{
tableName: 'tests',
timestamps: false,
}
);
Test.associate = (models) => {
Test.belongsToMany(models.Level, {
through: models.testHasLevel,
foreignKey: 'test_id',
otherKey: 'level_id',
timestamps: false,
});
Test.hasMany(models.Test, { foreignKey: 'parent_id', as: 'children' });
};
return Test;
};
模型:测试有模型
module.exports = (sequelize, DataTypes) => {
const testHasLevel = sequelize.define(
'testHasLevel',
{},
{
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
tableName: 'test_has_level',
timestamps: false,
}
);
testHasLevel.associate = (models) => {
testHasLevel.belongsTo(models.Test, {
foreignKey: 'test_id',
targetKey: 'test_id',
});
testHasLevel.belongsTo(models.Level, {
foreignKey: 'level_id',
targetKey: 'level_id',
});
};
return testHasLevel;
};
我还制作了 SESSION MODEL :
module.exports = (sequelize, DataTypes) => {
const Session = sequelize.define(
'Session',
{
session_id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
institut_id: {
type: DataTypes.INTEGER,
},
start: {
type: DataTypes.DATE,
},
end: {
type: DataTypes.DATE,
},
test_id: {
type: DataTypes.INTEGER,
},
level_id: {
type: DataTypes.INTEGER,
},
limitDateSubscribe: {
type: DataTypes.DATE,
},
placeAvailable: {
type: DataTypes.INTEGER,
},
},
{
tableName: 'sessions',
timestamps: false,
}
);
Session.associate = (models) => {
Session.hasMany(models.sessionHasUser, { foreignKey: 'session_id' });
};
return Session;
};
但我不知道如何“绑定(bind)”
SESSION
与
TEST_HAS_LEVEL
与 Sequelize ....
最佳答案
型号:水平
module.exports = (sequelize, DataTypes) => {
const Level = sequelize.define(
"Level",
{
level_id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
label: {
type: DataTypes.STRING,
allowNull: false,
unique: {
args: true,
msg: "Level:Label already exist!",
},
validate: {
notEmpty: { msg: `Level:Label cannot be empty!` },
notNull: { msg: `Level:Label cannot be NULL!` },
},
},
ref: {
type: DataTypes.STRING,
allowNull: true,
},
description: {
type: DataTypes.TEXT,
allowNull: true,
},
},
{
tableName: "levels",
timestamps: false,
}
);
Level.associate = (models) => {
Level.hasMany(models.testHasLevel, {
foreignKey: "level_level_id",
as: "levels",
});
};
return Level;
};
module.exports = (sequelize, DataTypes) => {
const Test = sequelize.define(
"Test",
{
test_id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
label: {
type: DataTypes.STRING,
allowNull: false,
validate: {
notEmpty: { msg: `Test:label cannot be empty!` },
notNull: { msg: `Test:label cannot be NULL!` },
},
},
isInternal: {
type: DataTypes.BOOLEAN,
defaultValue: false,
allowNull: false,
validate: {
notEmpty: { msg: `Test:isInternal cannot be empty!` },
notNull: { msg: `Test:isInternal cannot be NULL!` },
},
},
parent_id: {
type: DataTypes.INTEGER,
defaultValue: null,
allowNull: true,
},
},
{
tableName: "tests",
timestamps: false,
}
);
Test.associate = (models) => {
Test.hasMany(models.testHasLevel, {
foreignKey: "test_test_id",
as: "tests",
});
Test.hasMany(models.Test, { foreignKey: "parent_id", as: "children" });
};
return Test;
};
module.exports = (sequelize, DataTypes) => {
const testHasLevel = sequelize.define(
"testHasLevel",
{
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
test_test_id: {
type: DataTypes.INTEGER,
},
level_level_id: {
type: DataTypes.INTEGER,
},
},
{
tableName: "test_has_level",
timestamps: false,
}
);
testHasLevel.associate = (models) => {
testHasLevel.belongsTo(models.Test, {
foreignKey: "test_test_id",
as: "tests",
});
testHasLevel.belongsTo(models.Level, {
foreignKey: "level_level_id",
as: "levels",
});
testHasLevel.hasMany(models.Session, {
foreignKey: "test_has_level_id",
as: "test_has_level",
});
};
return testHasLevel;
};
module.exports = (sequelize, DataTypes) => {
const Session = sequelize.define(
"Session",
{
session_id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
institut_id: {
type: DataTypes.INTEGER,
},
start: {
type: DataTypes.DATE,
},
end: {
type: DataTypes.DATE,
},
test_has_level_id: {
type: DataTypes.INTEGER,
},
limitDateSubscribe: {
type: DataTypes.DATE,
},
placeAvailable: {
type: DataTypes.INTEGER,
},
},
{
tableName: "sessions",
timestamps: false,
}
);
Session.associate = (models) => {
Session.belongsTo(models.testHasLevel, {
foreignKey: "test_has_level_id",
as: "test_has_level",
});
};
return Session;
};
关于javascript - Sequelize : Many To Many table(CROSS TABLE) associated to other table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67114098/
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 8 年前。 Improve
我有四个模型类: class Group :event_producer end class PersonalBlog :event_producer end class Event true
将应用程序从Rails 4.2.9升级到Rails 5.2.1。 通过大部分令人讨厌的部分,更新了依赖关系和诸如此类的东西,最终使应用程序在console中运行,现在尝试访问server上的页面。加载
我的 EntityDefinition 中有一个关联: ... class ParentEntityDefinition extends EntityDefinition { ...
我不确定为什么这个关联无效 class Tag < ActiveRecord::Base has_and_belongs_to_many :routes end class Route < Act
我有以下关联。 PropertyOwner 是一个连接模型,它属于一个属性(property),多态地属于一个所有者,在下面的例子中是一个 ForeclosureDefense。一切正常,直到我拥有
我有一份有很多发票的工作,一张发票属于一份工作。我想查询第一张发票在某个日期范围内的工作。我有这个查询: @jobs = Job.joins(:invoices). where("invoices
我有这样的关系:用户可以拥有零只或一只狗,但狗必须属于某人。 # dog.rb class Dog { joins(:dog) } # To get records without a dog,
在我的 Rails 4 应用程序中,我有以下模型: User has_many :administrations has_many :calendars, through: :administrati
我见过的所有示例,包括文档都建议按关联过滤应使用以下语法 [contrived exampled] User.findAndCountAll({ include: [ {
我有一个下拉列表(HTML 选择框),它从这个 MySQL 查询中获取值: "SELECT cdID, cdTitle FROM CD ORDER BY cdID" 然后将结果存储在关联数组中,然后将
我是 Ruby ON Rails 新手,我的应用有 3 个模型 class User < ActiveRecord::Base has_one :user_hobby has_one :user_
我有三个模型,每个模型都有以下关联: class Model1 :model1 # will this work? is there any way around this? end class
我有一个带有帖子和标签的数据库。我想找到的帖子,只包含一个标签与一个特定的TagID,根本没有其他标签。下面的代码可以工作,但它需要服务器手动过滤结果,这将使分页变得困难。有谁有可以做到这一点的直接查
任何人都知道如何避免 PLS-00312 错误? “PLS-00312:位置参数关联可能不遵循命名关联” 我得到这个是因为下面一行: AttachList=> v_est_proc_name||'_E
我有以下工厂定义。 Factory.define :status do |f| end Factory.define :my_status , :parent => :status do |f|
我有 2 个具有 1:M 关联的模型,定义如下: var inventory = sequelize.define("Inventory",{**model definition**}; var tr
假设这个模式: var user = sequelize.define('user', { username: Sequelize.STRING, email: Sequelize.S
我正在使用 Apple 的工具进行应用站点关联验证,该工具位于此处:https://search.developer.apple.com/appsearch-validation-tool 它给了我错
实体\识别 /** * @ORM\Entity * @ORM\Table(name="c_rcgntn") */ class Recognition { /** * @ORM\
我是一名优秀的程序员,十分优秀!