Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MYSQL上的SailsJS协会_Mysql_Associations_Sails.js - Fatal编程技术网

MYSQL上的SailsJS协会

MYSQL上的SailsJS协会,mysql,associations,sails.js,Mysql,Associations,Sails.js,我有一个以前用于生产的MySQL数据库,现在我们的团队正在迁移到SailsJS。我读过关于帆船协会的书,我认为它很棒。但是我想知道您是否可以使用关联来使用.populate方法填充连接的表。我已经尝试过添加 user: { model: 'User' } 我的个人资料模型。但是当我尝试使用populate方法填充它时。它会产生一个错误 "error": "E_UNKNOWN", "status": 500, "summary": "Encountered an unexpected er

我有一个以前用于生产的MySQL数据库,现在我们的团队正在迁移到SailsJS。我读过关于帆船协会的书,我认为它很棒。但是我想知道您是否可以使用关联来使用
.populate
方法填充连接的表。我已经尝试过添加

user: {
   model: 'User'
}
我的个人资料模型。但是当我尝试使用populate方法填充它时。它会产生一个错误

"error": "E_UNKNOWN",
"status": 500,
"summary": "Encountered an unexpected error",
"raw": {
  "code": "ER_BAD_FIELD_ERROR",
  "errno": 1054,
  "sqlState": "42S22",
  "index": 0
}
下面是两个mysql表模式 对于用户表:

 CREATE TABLE IF NOT EXISTS `user` (
   `userId` int(20) NOT NULL AUTO_INCREMENT,
  `email` varchar(40) NOT NULL,
  `password` varchar(60) NOT NULL,
  `locationId` int(20) DEFAULT NULL,
  `status` tinyint(4) NOT NULL COMMENT '0 - inactive, 1 - active, 2 - delete',
  `companyId` int(20) DEFAULT NULL,
  `createDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`userId`),
  UNIQUE KEY `email` (`email`),
  KEY `fk_locationId` (`locationId`),
  KEY `fk_orgId` (`companyId`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=259 ;
对于配置文件表:

CREATE TABLE IF NOT EXISTS `profile` (
  `profileId` int(11) NOT NULL AUTO_INCREMENT,
  `firstName` varchar(30) NOT NULL,
  `lastName` varchar(30) NOT NULL,
  `suffix` varchar(10) DEFAULT NULL,
  `nickName` varchar(25) DEFAULT NULL,
  `title` varchar(45) DEFAULT NULL COMMENT 'Name Title, e.g, Dr., Engr., etc\n',
  `userId` int(11) NOT NULL,
  `birthDate` date DEFAULT NULL,
  `phoneNumber` varchar(30) DEFAULT NULL,
  `dateUpdated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `image` tinyint(1) NOT NULL DEFAULT '0',
  `gender` tinyint(1) NOT NULL COMMENT '0 - female, 1 - male',
  `middleName` varchar(20) DEFAULT NULL,
  `telephoneNumber` varchar(30) DEFAULT NULL,
  PRIMARY KEY (`profileId`),
  KEY `fk_userId` (`userId`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=259 ;

ALTER TABLE `profile`
  ADD CONSTRAINT `fk_userId` FOREIGN KEY (`userId`) REFERENCES `user` (`userId`) ON DELETE CASCADE ON UPDATE CASCADE;
更新

模型


型号

看起来您可能没有使用最新版本的Sails beta(rc7),该版本可能为您提供了稍好的错误消息(如导致错误的字段)

在任何情况下,似乎都需要进行一些数据库迁移,以使Sails能够正常工作。您可以让Waterline为您执行此操作——事实上,如果您的连接配置中没有
migrate:safe
属性,Waterline将默认执行此操作。但在您的情况下,正如迁移项目时经常发生的那样,您已经有了一个模式,您可能不希望水线弄乱它。没问题——我们只需要在您的模型配置中调整一些设置,您就可以开始了

在api/models/Profile.js中:

module.exports = {
    autoPK: false,        // don't try and add a unique ID; we already have one 
    autoCreatedAt: false, // don't try and add a createdAt timestamp
    autoUpdatedAt: false, // don't try and add a updatedAt timestamp
    attributes: {
        profileId: {
            type: 'integer',
            primaryKey: true
        },
        user: {
            model: 'user',
            columnName: 'userId'
        },
        ...etc...
    }
}
module.exports = {
    autoPK: false,        // don't try and add a unique ID; we already have one 
    autoCreatedAt: false, // don't try and add a createdAt timestamp
    autoUpdatedAt: false, // don't try and add a updatedAt timestamp
    attributes: {
        userId: {
            type: 'integer',
            primaryKey: true
        }
        profile: {
            model: 'profile',
            columnName: 'profileId'
        },
        ...etc...
    }
}
在api/models/User.js中:

module.exports = {
    autoPK: false,        // don't try and add a unique ID; we already have one 
    autoCreatedAt: false, // don't try and add a createdAt timestamp
    autoUpdatedAt: false, // don't try and add a updatedAt timestamp
    attributes: {
        profileId: {
            type: 'integer',
            primaryKey: true
        },
        user: {
            model: 'user',
            columnName: 'userId'
        },
        ...etc...
    }
}
module.exports = {
    autoPK: false,        // don't try and add a unique ID; we already have one 
    autoCreatedAt: false, // don't try and add a createdAt timestamp
    autoUpdatedAt: false, // don't try and add a updatedAt timestamp
    attributes: {
        userId: {
            type: 'integer',
            primaryKey: true
        }
        profile: {
            model: 'profile',
            columnName: 'profileId'
        },
        ...etc...
    }
}

请注意,Sails目前不支持真正的一对一关系,因此,如果您希望以两种方式填充,则必须分别链接双方。也就是说,添加带有
user:1
的配置文件将不允许您执行
user.findOne(1).populate('profile')
;您必须明确地设置user#1上的
配置文件
键,才能使其正常工作。

请查看。你的语法是错误的;
model
属性应该是一个字符串,它是要关联的模型的标识,例如
model:'user'
Oh yes。我只是忘了在用户身上加上撇号。但是不行。它仍然不起作用。它需要是小写的
user
。它是小写的吗?另外,你能发布你看到的错误吗?无论如何,我不认为这是个问题,因为我指定了模型的第一个字母大写。这很重要。除非您在模型定义中特别设置了
identity
属性,否则无论全球化的类名是什么,模型标识都是小写的。请您解释一下这句话:
您必须显式地设置user#1上的profile键才能工作。
对不起,我对sails有点陌生。那么,这是否意味着
User.findOne(1).populate('profile')
仍将不起作用?上述代码将使您能够在SAIL中使用与当前数据库的关联。我只是注意到,对于
userId
设置为1的新配置文件记录,您可以执行
profile.findOne([new profile id])。填充('user')
,但不能自动执行
user.findOne(1)。填充('profile')
。为了使其工作,您仍然需要将用户1的
profileId
更新为新的概要文件记录;水线不会帮你的。这与多对一或多对多关联不同,在多对一或多对多关联中,您可以使用
via
键指定反向关联。完整的Sails关联文档。感谢您的回答和链接,顺便问一下,您给出的示例是真的如此还是您碰巧交换了两者?我还更新了我的个人资料和用户模型