【问题标题】:Node js: association not working in sails js version 1节点 js:关联在 Sails js 版本 1 中不起作用
【发布时间】:2018-10-02 04:42:24
【问题描述】:

请帮助我,我正在使用 Sails 版本 1,我想创建关联,但出现错误。

error: A hook (`orm`) failed to load!
error: Could not tear down the ORM hook.  Error details: Error: Invalid data store identity. No data store exist with that identity.
error: Failed to lift app: { userError: The attribute `id` on the `patients` model is an association but also specifies a type property. This is not needed and will be automatically determined.

所以我有 2 个模型(患者和预订),我想从预订中获得患者

//Patients.js

module.exports = {
tableName: 'patients',
primaryKey: 'id',
attributes: {
    id: {
        model: 'Bookings',
        type: 'number',
        unique: true
    }
},

};

// Bookings.js

module.exports = {
tableName: 'booking',
attributes: {
    patient: {
        collection: 'Patients',
        via: 'id'
    }
}

};

【问题讨论】:

    标签: mysql node.js sails.js associations one-to-one


    【解决方案1】:

    按照错误提示,从Patients.js 中的attributes 键中删除type 属性。

    // Patients
    module.exports = {
    tableName: 'patients',
    primaryKey: 'id',
    attributes: {
     id: {
        type: 'number',
        unique: true
        },
     RandomAssociationName: {
        model: 'Bookings'
        }
    }
    
    // Bookings
    module.exports = {
    tableName: 'booking',
    attributes: {
    patient: {
        collection: 'Patients',
        via: 'RandomAssociationName'
      }
    }
    

    【讨论】:

    • 如果我删除类型键我得到另一个错误“类型必须是数字或字符串”
    • 为什么要将关联定义为主键?,您可以使用两个属性,一个作为主键,另一个表示关联,这样您设置模型应该没有问题。
    • 对不起@Hamza Fatmi,但我不明白你说“代表协会”是什么意思。你能把代码示例或更多详细信息发给我吗
    • @LevonBabayan 在Patient模型中,可以设置两个属性,一个作为模型的主键,另一个包含Booking模型的外键,见更新答案.
    • 非常感谢您,我认为这种方式确实有效,我明天尝试并写信给您
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    • 2016-04-16
    • 2018-09-07
    • 1970-01-01
    • 2016-03-18
    相关资源
    最近更新 更多