我想用sqlalchemy做orm:
我有两个表具有一对多关系,例如:
class Category(db.Model):
__tablename__ = 'Category'
id = sqdb.Column(postgresql.UUID(as_uuid=True), primary_key=True)
name = sqdb.Column(sqdb.String(80), nullable=False)
productions=relationship("Product", back_populates='Category')
def __init__(self, **kwargs):
db.Model.__init__(self, **kwargs)
class Product(db.Model):
__tablename__ = 'Product'
id = sqdb.Column(postgresql.UUID, primary_key=True)
name = sqdb.Column(sqdb.String(80), nullable=False)
desc = sqdb.Column(sqdb.TEXT, nullable=True)
provider = sqdb.Column(sqdb.String(200), nullable=False)
barcode = sqdb.Column(sqdb.String(20), nullable=False)
price = sqdb.Column(sqdb.DECIMAL(10, 2), nullable=False)
category_id=sqdb.Column(postgresql.UUID, sqdb.ForeignKey('Category.id'))
category = relationship("Category", back_populates='Product')
def __init__(self, **kwargs):
db.Model.__init__(self, **kwargs)
我想像伙伴一样初始化类别
Category(id="12323232", name="cooker")
实际上,我想用json文件初始化Category
jsonobj=json.loads(jsondtr)
Category(**json)
但是由于我有关系列,所以出现错误:
回溯(最近通话最近): get_property中的文件“ F:\ personalprojects \ flempty \ venv \ lib \ site-packages \ sqlalchemy \ orm \ mapper.py”,行1926 返回self._props [key] KeyError:“产品”
在处理上述异常期间,发生了另一个异常:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "<string>", line 2, in __init__
File "F:\personalprojects\flempty\venv\lib\site-packages\sqlalchemy\orm\instrumentation.py", line 391, in _new_state_if_none
state = self._state_constructor(instance, self)
File "F:\personalprojects\flempty\venv\lib\site-packages\sqlalchemy\util\langhelpers.py", line 767, in __get__
obj.__dict__[self.__name__] = result = self.fget(obj)
File "F:\personalprojects\flempty\venv\lib\site-packages\sqlalchemy\orm\instrumentation.py", line 221, in _state_constructor
self.dispatch.first_init(self, self.class_)
File "F:\personalprojects\flempty\venv\lib\site-packages\sqlalchemy\event\attr.py", line 284, in __call__
fn(*args, **kw)
File "F:\personalprojects\flempty\venv\lib\site-packages\sqlalchemy\orm\mapper.py", line 3143, in _event_on_first_init
configure_mappers()
File "F:\personalprojects\flempty\venv\lib\site-packages\sqlalchemy\orm\mapper.py", line 3033, in configure_mappers
mapper._post_configure_properties()
File "F:\personalprojects\flempty\venv\lib\site-packages\sqlalchemy\orm\mapper.py", line 1832, in _post_configure_properties
prop.init()
File "F:\personalprojects\flempty\venv\lib\site-packages\sqlalchemy\orm\interfaces.py", line 183, in init
self.do_init()
File "F:\personalprojects\flempty\venv\lib\site-packages\sqlalchemy\orm\relationships.py", line 1659, in do_init
self._generate_backref()
File "F:\personalprojects\flempty\venv\lib\site-packages\sqlalchemy\orm\relationships.py", line 1892, in _generate_backref
self._add_reverse_property(self.back_populates)
File "F:\personalprojects\flempty\venv\lib\site-packages\sqlalchemy\orm\relationships.py", line 1599, in _add_reverse_property
other = self.mapper.get_property(key, _configure_mappers=False)
File "F:\personalprojects\flempty\venv\lib\site-packages\sqlalchemy\orm\mapper.py", line 1929, in get_property
"Mapper '%s' has no property '%s'" % (self, key))
sqlalchemy.exc.InvalidRequestError: Mapper 'Mapper|Category|Category' has no property 'Product
如何删除类别中的“生产”密钥或产品中的类别密钥 init 函数还是要在模型中进行初始化?
您能举个例子吗?