app.model.define('news')
//获取model
app.model.news
app.model.News
都是undefined?
第二个问题,sequelize v4了,我看现在egg-sequelize还是3.x,会升级嘛?
1: 好好看文档
// app/model/post.js
module.exports = app => {
const { STRING, INTEGER, DATE } = app.Sequelize;
return app.model.define('Post', {
name: STRING(30),
user_id: INTEGER,
created_at: DATE,
updated_at: DATE,
}, {
classMethods: {
associate() {
app.model.Post.belongsTo(app.model.User, { as: 'user' });
}
}
});
};
源码是遍历model文件夹下面的文件,在controller里可以 ctx.model.Post 拿到(首字母大写)对象。
2: @atian25 希望可以升级到v4,sequelize的v3文档已经404了,另外依赖变成了mysql2,这个在egg的文档有出现也需要更新。
@solarhell 我按照官方的方式写的。所以出问题了,抱歉。不过我发现好像belongsTo或者hasmany没有添加相应的字段。
module.exports = app => {
const { STRING, INTEGER, BOOLEAN, ENUM } = app.Sequelize;
const News = app.model.define('news',{
title: {
type: STRING(40),
allowNull:true
},
content:{
type: STRING,
allowNull:true
},
}, {
classMethods: {
associate() {
app.model.News.belongsTo(app.model.NewsCategory,{as:"category"});
}
}
});
News.sync({
force:true
});
return News;
}
CREATE TABLE IF NOT EXISTS "news" ("id" SERIAL , "title" VARCHAR(40), "content" VARCHAR(255), "created_at" TIMESTAMP WITH TIME ZONE NOT NULL, "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL, PRIMARY KEY ("id")); (53ms)
md5-e50ce2a5fcaa5d238806bf80be26dc4c
-- auto-generated definition
create table news
(
id serial not null
constraint news_pkey
primary key,
title varchar(40),
content varchar(255),
created_at timestamp with time zone not null,
updated_at timestamp with time zone not null
)
;
你的写法不对,sync工作不应该在model定义的时候做。
module.exports = app => {
const { STRING, INTEGER, BOOLEAN, ENUM } = app.Sequelize;
return app.model.define('news',{
title: {
type: STRING(40),
allowNull:true
},
content:{
type: STRING,
allowNull:true
},
}, {
classMethods: {
associate() {
app.model.News.belongsTo(app.model.NewsCategory,{as:"category"});
}
}
});
}
sync工作应该放在app.js中:
'use strict';
module.exports = app => {
app.beforeStart(function* () {
yield app.model.sync({force: true});
});
};
需要完整的例子可以参照我的项目:seqeulize-example
@huacnlee 看看
我加个文档,sync的问题问了好几次了。。
@iyuq 欢迎把这个example pr 到 egg-sequelize 主仓库里
@jtyjty99999 可以的,我去提个PR。
放 https://github.com/eggjs/examples 怎么样? @jtyjty99999
@atian25 我觉得蛮好的 @iyuq 要不放在这里吧
@jtyjty99999 @atian25 好的,我下午弄下。
什么时候在更新下到v4?
我建议用 Migration 来管理数据库结构,而不是用 sync 函数。
尝试自己fork了下egg-sequelize,升级到v4,但是classMethods和instanceMethods都被移除了,没能跑通测试。希望可以关注下,谢谢。
@huacnlee ok~
有没有自动生成这些models的工具哦?
同表示升级到 v4 后,classMethods 和 instanceMethods 被移除了,在 app.model 中读取的时候(存在先后问题?)导致获取 model 的时候会出现 undefined
@sqrthree 请看这里 https://github.com/eggjs/egg-sequelize/blob/master/README.md 文档已更新。
ModelName.prototype.xxxx 或许这是定义实例方法的写法?
@solarhell 我刚测试了下,跑文档上的例子也会报错哎:
module.exports = app => {
const { STRING, INTEGER, DATE } = app.Sequelize;
const Post = app.model.define('Post', {
name: STRING(30),
user_id: INTEGER,
created_at: DATE,
updated_at: DATE,
});
Post.associate = function() {
console.log(app.model.User)
app.model.Post.belongsTo(app.model.User, { as: 'user' });
}
return Post;
};
app.model.User 会打印出 undefined,然后就出现如下错误了:
if (!target.prototype || !(target.prototype instanceof this.sequelize.Model)) {
^
TypeError: Cannot read property 'prototype' of undefined
@sqrthree 等发新版本吧,现在是有BUG。
好的
已发布 3.0.1
Most helpful comment
已发布 3.0.1