无法使用关联函数-IntellIj问题

时间:2019-04-22 12:36:35

标签: javascript intellij-idea sequelize.js

更新

今天早上,我想使用另一个内置的sequelize函数,突然belongsTo()和hasMany()函数可用并在某些模型上工作-并非全部!这些模型的样式完全相同,只是名称不同。

例如hasOne不适用于topicClass

enter image description here

我什么都没改变。除了我重新启动了IntelliJ。在打开这个问题之前,我确实重新启动了它,并且始终遇到相同的问题。所以我认为这个问题是基于IDE的!还是很高兴提示如何避免这种情况


我有一个与SQLite一起使用续集的APP。

当我想在我的db-controller.js中建立关联时,它告诉我docu中的belongsTohasmany之类的关联功能不可用。

当我执行setupAssociations函数并稍后检查数据库时,有模型但未设置关联。

在这里db-controller.js(在底部的模特之后,我的身边是更多信息!)

const Sequelize = require('sequelize')
const userModel = require('../model/user')
const subjectModel = require('../model/subject')
// eslint-disable-next-line no-unused-vars
const initUser = userModel.user.initUser()
// eslint-disable-next-line no-unused-vars
const initSubject = subjectModel.subject.initSubject()
const sequelize = new Sequelize({
  dialect: 'sqlite',
  storage: './user.sqlite',
  pool: {
    max: 5,
    min: 0,
    acquire: 30000,
    idle: 10000
  },
  logging: false
})
sequelize.sync()

function setupAssociations(){
  // user + subject
  userModel.user.userClass.hasMany(subjectModel.subject.subjectClass)
  subjectModel.subject.subjectClass.belongsTo(userModel.user.userClass)
  // subject + topic
}

function testAssociations(){
  setupAssociations()
}

testAssociations()

和我的两个模型

const Sequelize = require('sequelize')
const sequelize = new Sequelize({
  dialect: 'sqlite',
  storage: '../controller/user.sqlite',
  pool: {
    max: 5,
    min: 0,
    acquire: 30000,
    idle: 10000
  },
  logging: false
})
sequelize.sync()

/**
 * User Model
 */
class User extends Sequelize.Model {
}


/**
 * Init user model
 */

function initUser () {
  //TODO optional realize with sequelize.transaction
  User.init(
    // attributes
    {
      firstName: {
        type: Sequelize.STRING,
        allowNull: false
      },
      lastName: {
        type: Sequelize.STRING,
        allowNull: false
      },
      email: {
        type: Sequelize.STRING,
        allowNull: false
      },
      title: {
        type: Sequelize.STRING,
        allowNull: false
      },
      password: {
        type: Sequelize.TEXT,
        allowNull: false
      }
    },
    // options
    {
      sequelize,
      modelName: 'user'
    }
  )

}

/**
 * Makes functions available globally
 */
exports.user = {
  initUser: initUser,
  userClass: User
}

和主题

const Sequelize = require('sequelize')
const sequelize = new Sequelize({
  dialect: 'sqlite',
  storage: '../controller/user.sqlite',
  pool: {
    max: 5,
    min: 0,
    acquire: 30000,
    idle: 10000
  },
  logging: false
})
sequelize.sync()

/**
 // * Subject Model
 */
class Subject extends Sequelize.Model {
}

/**
 * Initialises subject model
 */
function initSubject () {
  Subject.init(
    // attributes
    {
      subjectName: {
        type: Sequelize.STRING,
        allowNull: false
      }
    },
    // options
    {
      sequelize,
      modelName: 'subject'
    }
  )
}

/**
 * Makes functions globally available
 */
exports.subject = {
  initSubject: initSubject,
  subjectClass: Subject
}

如果我在class TestClass extends Sequelize.Model文件中创建另一个类似user.js的类,并在hasMany函数中用belongsTo调用initUser并随后检查数据库,则一切都如我所愿。

所以我在这里做错了吗?

1 个答案:

答案 0 :(得分:0)

我发现我从另一个项目中运行了一个脚本,该脚本正在添加到导出功能的注释中

public async Task Invoke(HttpContext context, IYourDependency someObject)
{
    await _requestDelegate(context);
}

这引起了问题。删除它会使所有续集功能再次出现。