修复出库时候找不到名称等问题

This commit is contained in:
dxc
2026-02-10 11:13:07 +08:00
parent bccbeaadce
commit e876505a1b
7 changed files with 49 additions and 40 deletions

View File

@ -32,14 +32,14 @@ class MaterialBase(db.Model):
# 关联关系区域
# ============================================================
# 1. 关联采购库存 (StockBuy)
stock_buys = db.relationship('StockBuy', back_populates='material', lazy='dynamic')
# 1. 关联采购库存 (StockBuy) - 修改 back_populates 为 'base'
stock_buys = db.relationship('StockBuy', back_populates='base', lazy='dynamic')
# 2. 关联半成品库存 (StockSemi)
stock_semis = db.relationship('StockSemi', back_populates='material', lazy='dynamic')
# 2. 关联半成品库存 (StockSemi) - 修改 back_populates 为 'base'
stock_semis = db.relationship('StockSemi', back_populates='base', lazy='dynamic')
# 3. 关联成品库存 (StockProduct)
stock_products = db.relationship('StockProduct', back_populates='material', lazy='dynamic')
# 3. 关联成品库存 (StockProduct) - 修改 back_populates 为 'base'
stock_products = db.relationship('StockProduct', back_populates='base', lazy='dynamic')
def to_dict(self):
"""

View File

@ -1,5 +1,8 @@
# inventory-backend/app/models/inbound/buy.py
from app.extensions import db
import json
# 显式导入 MaterialBase 以防 relationship 找不到引用
from app.models.base import MaterialBase
class StockBuy(db.Model):
"""
@ -48,9 +51,8 @@ class StockBuy(db.Model):
# [新增] 全局打印流水号 (用于跨表连续编号,对应 Sequence: global_print_seq)
global_print_id = db.Column(db.Integer)
# 关系定义
# 注意:这里使用字符串 'MaterialBase' 引用,避免了直接 import 导致的潜在循环依赖
material = db.relationship('MaterialBase', back_populates='stock_buys')
# 关系定义 [已修改]
base = db.relationship('MaterialBase', back_populates='stock_buys')
def to_dict(self):
# 辅助解析函数:将数据库存储的 JSON 字符串转为 List
@ -68,11 +70,12 @@ class StockBuy(db.Model):
return {
'id': self.id,
'base_id': self.base_id,
'material_name': self.material.name if self.material else '',
'spec_model': self.material.spec_model if self.material else '',
'category': self.material.category if self.material else '',
'unit': self.material.unit if self.material else '',
'material_type': self.material.material_type if self.material else '',
# [已修改] 使用 self.base
'material_name': self.base.name if self.base else '',
'spec_model': self.base.spec_model if self.base else '',
'category': self.base.category if self.base else '',
'unit': self.base.unit if self.base else '',
'material_type': self.base.material_type if self.base else '',
'sku': self.sku,
'inbound_date': self.in_date.strftime('%Y-%m-%d') if self.in_date else '',

View File

@ -1,7 +1,7 @@
# app/models/inbound/product.py
from app.extensions import db
import json
from app.models.base import MaterialBase
class StockProduct(db.Model):
"""
@ -57,8 +57,8 @@ class StockProduct(db.Model):
# 全局打印流水号
global_print_id = db.Column(db.Integer)
# 关系定义
material = db.relationship('MaterialBase', back_populates='stock_products')
# 关系定义 [已修改]
base = db.relationship('MaterialBase', back_populates='stock_products')
def to_dict(self):
raw_val = float(self.raw_material_cost or 0)
@ -79,11 +79,12 @@ class StockProduct(db.Model):
return {
'id': self.id,
'base_id': self.base_id,
'material_name': self.material.name if self.material else '',
'spec_model': self.material.spec_model if self.material else '',
'category': self.material.category if self.material else '',
'unit': self.material.unit if self.material else '',
'material_type': self.material.material_type if self.material else '',
# [已修改] 使用 self.base
'material_name': self.base.name if self.base else '',
'spec_model': self.base.spec_model if self.base else '',
'category': self.base.category if self.base else '',
'unit': self.base.unit if self.base else '',
'material_type': self.base.material_type if self.base else '',
'sku': self.sku,
'inbound_date': self.production_date.strftime('%Y-%m-%d') if self.production_date else '',

View File

@ -1,7 +1,7 @@
# app/models/inbound/semi.py
from app.extensions import db
import json
from app.models.base import MaterialBase
class StockSemi(db.Model):
"""
@ -55,8 +55,8 @@ class StockSemi(db.Model):
# [新增] 全局打印流水号
global_print_id = db.Column(db.Integer)
# 关系定义
material = db.relationship('MaterialBase', back_populates='stock_semis')
# 关系定义 [已修改]
base = db.relationship('MaterialBase', back_populates='stock_semis')
def to_dict(self):
raw_val = float(self.raw_material_cost or 0)
@ -78,11 +78,12 @@ class StockSemi(db.Model):
return {
'id': self.id,
'base_id': self.base_id,
'material_name': self.material.name if self.material else '',
'spec_model': self.material.spec_model if self.material else '',
'category': self.material.category if self.material else '',
'unit': self.material.unit if self.material else '',
'material_type': self.material.material_type if self.material else '',
# [已修改] 使用 self.base
'material_name': self.base.name if self.base else '',
'spec_model': self.base.spec_model if self.base else '',
'category': self.base.category if self.base else '',
'unit': self.base.unit if self.base else '',
'material_type': self.base.material_type if self.base else '',
'sku': self.sku,
'inbound_date': self.production_date.strftime('%Y-%m-%d') if self.production_date else '',