我有一个以前用于生产的 MySQL 数据库,现在我们的团队正在迁移到 SailsJS。我读过有关 sails 船协会的资料,我认为它很棒。但我想知道您是否可以使用关联来使用 .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;
更新
User型号
Profile型号
最佳答案
看起来您可能没有使用最新版本的 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...
}
}
在 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: {
userId: {
type: 'integer',
primaryKey: true
}
profile: {
model: 'profile',
columnName: 'profileId'
},
...etc...
}
}
请注意,Sails 当前不支持真正的一对一关系,因此如果您想要填充两种方式,则必须分别链接双方。也就是说,使用 user: 1 添加配置文件不会让您执行 User.findOne(1).populate('profile');您必须在用户 #1 上显式设置 profile 键才能使其正常工作。
关于mysql - MYSQL 上的 SailsJS 协会,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23732504/
我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问
我将我的Rails应用程序部署到OpenShift,它运行良好,但我无法在生产服务器上运行“Rails控制台”。它给了我这个错误。我该如何解决这个问题?我尝试更新rubygems,但它也给出了权限被拒绝的错误,我也无法做到。railsc错误:Warning:You'reusingRubygems1.8.24withSpring.UpgradetoatleastRubygems2.1.0andrun`gempristine--all`forbetterstartupperformance./opt/rh/ruby193/root/usr/share/rubygems/rubygems
我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que
文章目录一、概述简介原理模块二、配置Mysql使用版本环境要求1.操作系统2.mysql要求三、配置canal-server离线下载在线下载上传解压修改配置单机配置集群配置分库分表配置1.修改全局配置2.实例配置垂直分库水平分库3.修改group-instance.xml4.启动监听四、配置canal-adapter1修改启动配置2配置映射文件3启动ES数据同步查询所有订阅同步数据同步开关启动4.验证五、配置canal-admin一、概述简介canal是Alibaba旗下的一款开源项目,Java开发。基于数据库增量日志解析,提供增量数据订阅&消费。Git地址:https://github.co
我有一个.pfx格式的证书,我需要使用ruby提取公共(public)、私有(private)和CA证书。使用shell我可以这样做:#ExtractPublicKey(askforpassword)opensslpkcs12-infile.pfx-outfile_public.pem-clcerts-nokeys#ExtractCertificateAuthorityKey(askforpassword)opensslpkcs12-infile.pfx-outfile_ca.pem-cacerts-nokeys#ExtractPrivateKey(askforpassword)o
我发现自己需要这个。假设cart是一个包含用户列表的模型。defindex_of_itemcart.users.each_with_indexdo|u,i|ifu==current_userreturniendend获取此类关联索引的更简单方法是什么? 最佳答案 indexArray上的方法与您的index_of_item方法相同,例如cart.users.index(current_user)返回数组中第一个对象的索引==给obj。如果未找到匹配项,则返回nil。 关于ruby-on-
我了解instance_eval和class_eval之间的基本区别。我在玩弄时发现的是一些涉及attr_accessor的奇怪东西。这是一个例子:A=Class.newA.class_eval{attr_accessor:x}a=A.newa.x="x"a.x=>"x"#...expectedA.instance_eval{attr_accessor:y}A.y="y"=>NoMethodError:undefinedmethod`y='forA:Classa.y="y"=>"y"#WHATTT?这是怎么回事:instance_eval没有访问我们的A类(对象)然后它实际上将它添加到
我有一个集合选择:此方法的单选按钮是什么?谢谢 最佳答案 Rails3中没有这样的助手。在Rails4中,它是collection_radio_buttons. 关于ruby-on-rails-rails上的ruby:radiobuttonsforcollectionselect,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/18525986/
我看到其他人也遇到过类似的问题,但没有一个解决方案对我有用。0.3.14gem与其他gem文件一起存在。我已经完全按照此处指示完成了所有操作:https://github.com/brianmario/mysql2.我仍然得到以下信息。我不知道为什么安装程序指示它找不到include目录,因为我已经检查过它存在。thread.h文件存在,但不在ruby目录中。相反,它在这里:C:\RailsInstaller\DevKit\lib\perl5\5.8\msys\CORE\我正在运行Windows7并尝试在Aptana3中构建我的Rails项目。我的Ruby是1.9.3。$gemin
我已经开始使用mysql2gem。我试图弄清楚一些基本的事情——其中之一是如何明确地执行事务(对于批处理操作,比如多个INSERT/UPDATE查询)。在旧的ruby-mysql中,这是我的方法:client=Mysql.real_connect(...)inserts=["INSERTINTO...","UPDATE..WHEREid=..",#etc]client.autocommit(false)inserts.eachdo|ins|beginclient.query(ins)rescue#handleerrorsorabortentirelyendendclient.commi