sequelize 多对多模型关系中 新增数据问题
定义好模型后,sequelize会在数据库中生产3张表,客户表、公司表、客户公司关联表、 请问如何在新建客户的时候,同时将客户和公司的关联数据新增到中间表中去 我这样编写然后报错了
10 回复
你有 console.log
出來看看 result 是否有值嗎?
@Rukeith 有的
customer {
dataValues:
{ id: 22,
name: '123',
userName: '133',
phone: '23333322233',
sex: '男',
createTime: 2017-09-28T05:10:30.961Z,
createId: 4 },
_previousDataValues:
{ name: '123',
userName: '133',
phone: '23333322233',
sex: '男',
createTime: 2017-09-28T05:10:30.961Z,
createId: 4,
id: 22,
password: undefined,
photoUrl: undefined,
editTime: undefined,
deleteTime: undefined,
editId: undefined,
deleteId: undefined },
_changed:
{ name: false,
userName: false,
phone: false,
sex: false,
createTime: false,
createId: false,
id: false,
password: false,
photoUrl: false,
editTime: false,
deleteTime: false,
editId: false,
deleteId: false },
_modelOptions:
{ timestamps: false,
validate: {},
freezeTableName: false,
underscored: false,
underscoredAll: false,
paranoid: false,
rejectOnEmpty: false,
whereCollection: null,
schema: null,
schemaDelimiter: '',
defaultScope: {},
scopes: [],
hooks: {},
indexes: [],
name: { plural: 'customers', singular: 'customer' },
omitNull: false,
tableName: 'customer',
sequelize:
Sequelize {
options: [Object],
config: [Object],
dialect: [Object],
queryInterface: [Object],
models: [Object],
modelManager: [Object],
connectionManager: [Object],
importCache: [Object],
test: [Object] },
uniqueKeys: { customer_phone_unique: [Object] } },
_options:
{ isNewRecord: true,
_schema: null,
_schemaDelimiter: '',
attributes: undefined,
include: undefined,
raw: undefined,
silent: undefined },
__eagerlyLoadedAssociations: [],
isNewRecord: false }
或者谁有这方面的demo让我借鉴借鉴么
看起來他是沒有 setCusCompany
我的做法是把關聯表當作 Model 來操作
sequelize.customer_company.create(options);
不然應該也可以參考這邊的資料
@Rukeith 关联表是sequelize自动生成的 也能当做model来操作么, 我这里如果这样操作的话 会直接显示这个model undefined
@xumjs8623 你仔细看看文档,肯定有办法的。 自动生成的关联表你肯定操作不了的,那个是自动的
@hezhongfeng 好吧,谢谢你
我也碰到这个问题 A.create(values); A.addB({},{}); 提示A.addB is not a function
@zhang962976642 我暂时用原生sql语句写了,等项目忙完再回过头来研究了。。