id: 在 sequelize 中创建新项目时为 null
id: null when creating a new item in sequelize
当我尝试创建一个新的 Conversation 项目时,Sequelize 将 return 一个具有 id: null
的对象,尽管数据库中有一个有效的 ID。我怎样才能让 Sequelize 到 return 最后插入的 id 到新创建的项目?
Conversation.create({
type: 'private',
createdBy: 1,
}).then(conversation => {
reply(conversation);
});
会return
{
"type": "conversations",
"id": null,
"createdBy": 1,
"created_at": "2016-03-18T01:47:48.000Z"
}
我的代码:
const Conversation = model.define('Conversation', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
},
type: {
type: Sequelize.ENUM,
values: ['private', 'group'],
validate: {
isIn: ['private', 'group'],
},
},
createdBy: {
type: Sequelize.INTEGER,
field: 'created_by',
},
}, {
tableName: 'conversations',
timestamps: true,
createdAt: 'created_at',
updatedAt: false,
getterMethods: {
type: () => 'conversations',
},
});
const User = model.define('User', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
},
firstName: {
type: Sequelize.STRING,
field: 'first_name',
allowNull: false,
},
lastName: {
type: Sequelize.STRING,
field: 'last_name',
allowNull: true,
},
email: {
type: Sequelize.STRING,
allowNull: false,
},
profileImg: {
type: Sequelize.STRING,
field: 'profile_img',
allowNull: false,
},
password: Sequelize.STRING,
}, {
tableName: 'users',
timestamps: true,
createdAt: 'created_at',
updatedAt: 'updated_at',
getterMethods: {
type: () => 'users',
},
});
Conversation.belongsToMany(User, {
foreignKey: 'conversation_id',
otherKey: 'user_id',
through: 'conversation_user',
timestamps: false,
});
User.belongsToMany(Conversation, {
as: 'conversations',
foreignKey: 'user_id',
otherKey: 'conversation_id',
through: 'conversation_user',
timestamps: false,
});
你需要在 id
字段中输入 autoIncrement: true
:
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
}
我个人建议跳过 id
列,因为 sequalize
会自动为您完成并且效果很好。
希望对您有所帮助:)
问题出在我的 MySQL 版本(5.6 而不是 5.7),更新了它,现在我正在获取 promise 中创建的项目的 ID。
我不确定 Sequelize 如何与 id
字段一起工作,如果我这样做 instance.id
,我会得到 null
,bug如果我执行以下操作,我可以获得 DB 的实际价值:
console.info(instance.id); // null
console.info(instance.get('id')); // 25 => Real ID
console.info(instance.getDataValue('id')); // 25 => Real ID
createdAt
和 updatedAt
等其他领域也发生了类似的事情。
为了获得 id
字段和其他相关字段的真实值,我在 Model 声明中添加了以下逻辑:
class FooModel extends Model {
// ...
/**
* @inheritdoc
*/
public async save(options?: SaveOptions<TModelAttributes>): Promise<this> {
await super.save(options);
this.loadBaseData();
return this;
}
/**
* @inheritdoc
*/
public async reload(options?: FindOptions<TModelAttributes>): Promise<this> {
await super.reload(options);
this.loadBaseData();
return this;
}
private loadBaseData() {
this.id = this.getDataValue('id');
this.createdAt = this.getDataValue('createdAt');
this.updatedAt = this.getDataValue('updatedAt');
}
}
当我尝试创建一个新的 Conversation 项目时,Sequelize 将 return 一个具有 id: null
的对象,尽管数据库中有一个有效的 ID。我怎样才能让 Sequelize 到 return 最后插入的 id 到新创建的项目?
Conversation.create({
type: 'private',
createdBy: 1,
}).then(conversation => {
reply(conversation);
});
会return
{
"type": "conversations",
"id": null,
"createdBy": 1,
"created_at": "2016-03-18T01:47:48.000Z"
}
我的代码:
const Conversation = model.define('Conversation', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
},
type: {
type: Sequelize.ENUM,
values: ['private', 'group'],
validate: {
isIn: ['private', 'group'],
},
},
createdBy: {
type: Sequelize.INTEGER,
field: 'created_by',
},
}, {
tableName: 'conversations',
timestamps: true,
createdAt: 'created_at',
updatedAt: false,
getterMethods: {
type: () => 'conversations',
},
});
const User = model.define('User', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
},
firstName: {
type: Sequelize.STRING,
field: 'first_name',
allowNull: false,
},
lastName: {
type: Sequelize.STRING,
field: 'last_name',
allowNull: true,
},
email: {
type: Sequelize.STRING,
allowNull: false,
},
profileImg: {
type: Sequelize.STRING,
field: 'profile_img',
allowNull: false,
},
password: Sequelize.STRING,
}, {
tableName: 'users',
timestamps: true,
createdAt: 'created_at',
updatedAt: 'updated_at',
getterMethods: {
type: () => 'users',
},
});
Conversation.belongsToMany(User, {
foreignKey: 'conversation_id',
otherKey: 'user_id',
through: 'conversation_user',
timestamps: false,
});
User.belongsToMany(Conversation, {
as: 'conversations',
foreignKey: 'user_id',
otherKey: 'conversation_id',
through: 'conversation_user',
timestamps: false,
});
你需要在 id
字段中输入 autoIncrement: true
:
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
}
我个人建议跳过 id
列,因为 sequalize
会自动为您完成并且效果很好。
希望对您有所帮助:)
问题出在我的 MySQL 版本(5.6 而不是 5.7),更新了它,现在我正在获取 promise 中创建的项目的 ID。
我不确定 Sequelize 如何与 id
字段一起工作,如果我这样做 instance.id
,我会得到 null
,bug如果我执行以下操作,我可以获得 DB 的实际价值:
console.info(instance.id); // null
console.info(instance.get('id')); // 25 => Real ID
console.info(instance.getDataValue('id')); // 25 => Real ID
createdAt
和 updatedAt
等其他领域也发生了类似的事情。
为了获得 id
字段和其他相关字段的真实值,我在 Model 声明中添加了以下逻辑:
class FooModel extends Model {
// ...
/**
* @inheritdoc
*/
public async save(options?: SaveOptions<TModelAttributes>): Promise<this> {
await super.save(options);
this.loadBaseData();
return this;
}
/**
* @inheritdoc
*/
public async reload(options?: FindOptions<TModelAttributes>): Promise<this> {
await super.reload(options);
this.loadBaseData();
return this;
}
private loadBaseData() {
this.id = this.getDataValue('id');
this.createdAt = this.getDataValue('createdAt');
this.updatedAt = this.getDataValue('updatedAt');
}
}