Node.js sequelize 创建一个对象,虽然关联失败
Node.js sequelize Create an object although the association fails
我在 node.js 上使用 sequelize。
我的资产模型对象:
module.exports =
class Asset extends Model {
static init(sequelize) {
return super.init(
{
AssetID: {
field: "asset_id",
type: DataTypes.INTEGER(11),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
AssetName: {
field: "asset_name",
type: DataTypes.STRING(40),
allowNull: false
},
SKU: {
field: "sku",
type: DataTypes.STRING(30)
}
},
Object.assign({
sequelize,
tableName: "assets",
})
);
}
static associate(models) {
Asset.Listings = this.hasMany(models.Listing,
{
as: 'Listings',
foreignKey: {name: 'AssetID', field: 'asset_id'},
sourceKey: 'AssetID'
})
}
};
列表定义为:
module.exports =
class Listing extends Model {
static init(sequelize) {
return super.init(
{
ListingID: {
field: "listing_id",
type: DataTypes.INTEGER(11),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
IsPreferred: {
field: "isPreferred",
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false
},
Price: {
field: "price",
type: DataTypes.DOUBLE(5,2),
allowNull: false,
validate: { min: 0 }
},
ListingType: {
field: "listing_type",
type: DataTypes.STRING(20),
allowNull: false,
defaultValue: "Buy",
validate: {
isIn: [['Buy', 'Rent']]
}
}
},
Object.assign({
sequelize,
tableName: "listings",
})
);
}
这是我的创建代码:
await Asset.create(newRecord, {include: [{ association: Asset.Listings } ]})
.then(dbRecord => {
logger.debug(`New Asset created`);
return dbRecord;
})
.catch(err => {
logger.warn(`Asset could not be created. ${JSON.stringify(newRecord)}`, err);
throw createServiceError(`Create Asset`, err);
})
现在...我的问题是,当我使用列表关联创建新资产记录时,如果列表创建失败(例如在类型验证中,假设我输入“abc”),将不会创建列表(因为该值无效),但会创建资产。
我希望整个交易都将被还原。
有点奇怪,因为创建调用在 catch 块中结束。
有什么想法吗?
谢谢,
阿萨夫
您可以使用交易。
抱歉格式化我正在给出 phone.
的回答
sequelize.transaction(transaction => {
Asset.create(newRecord, {include: [{ association: Asset.Listings } ], transaction})
})
.then(function() {
console.log('success');
})
.catch(function(err) {
console.log(err);
})
})
我在 node.js 上使用 sequelize。
我的资产模型对象:
module.exports =
class Asset extends Model {
static init(sequelize) {
return super.init(
{
AssetID: {
field: "asset_id",
type: DataTypes.INTEGER(11),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
AssetName: {
field: "asset_name",
type: DataTypes.STRING(40),
allowNull: false
},
SKU: {
field: "sku",
type: DataTypes.STRING(30)
}
},
Object.assign({
sequelize,
tableName: "assets",
})
);
}
static associate(models) {
Asset.Listings = this.hasMany(models.Listing,
{
as: 'Listings',
foreignKey: {name: 'AssetID', field: 'asset_id'},
sourceKey: 'AssetID'
})
}
};
列表定义为:
module.exports =
class Listing extends Model {
static init(sequelize) {
return super.init(
{
ListingID: {
field: "listing_id",
type: DataTypes.INTEGER(11),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
IsPreferred: {
field: "isPreferred",
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false
},
Price: {
field: "price",
type: DataTypes.DOUBLE(5,2),
allowNull: false,
validate: { min: 0 }
},
ListingType: {
field: "listing_type",
type: DataTypes.STRING(20),
allowNull: false,
defaultValue: "Buy",
validate: {
isIn: [['Buy', 'Rent']]
}
}
},
Object.assign({
sequelize,
tableName: "listings",
})
);
}
这是我的创建代码:
await Asset.create(newRecord, {include: [{ association: Asset.Listings } ]})
.then(dbRecord => {
logger.debug(`New Asset created`);
return dbRecord;
})
.catch(err => {
logger.warn(`Asset could not be created. ${JSON.stringify(newRecord)}`, err);
throw createServiceError(`Create Asset`, err);
})
现在...我的问题是,当我使用列表关联创建新资产记录时,如果列表创建失败(例如在类型验证中,假设我输入“abc”),将不会创建列表(因为该值无效),但会创建资产。 我希望整个交易都将被还原。
有点奇怪,因为创建调用在 catch 块中结束。
有什么想法吗?
谢谢, 阿萨夫
您可以使用交易。 抱歉格式化我正在给出 phone.
的回答sequelize.transaction(transaction => {
Asset.create(newRecord, {include: [{ association: Asset.Listings } ], transaction})
})
.then(function() {
console.log('success');
})
.catch(function(err) {
console.log(err);
})
})