如何在 Elixir 中保存模型
我已经使用 Django 模型创建了一个数据库,现在我正在使用 SQLAlchemy 和 Elixir 访问该数据库。查询有效,我可以完美地从数据库中提取项目,但是当我编辑它们并尝试保存它们时,它会抛出以下异常:
>>> p = Problem.query.first()
>>> p
<Problem('Test Problem', 'This is a test problem so the database has something in it', 'SBMT')>
>>> p.name
u'Test Problem'
>>> p.name = "Test_Problem"
>>> p.save()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/site-packages/elixir/entity.py", line 1116, in save
return self._global_session.save(self, *args, **kwargs)
AttributeError: 'Session' object has no attribute 'save'
我做错了什么?我是否错过了设置中阻止我将内容保存到数据库的关键部分,或者是我的 elixir 和 SQLAlchemy 版本有问题?
我已经运行 setup_all()
并且 metadata.bind
已全部设置,因此我可以查询数据库。
I've got a database created using Django models which I'm now accessing using SQLAlchemy and Elixir. The querying works and I can pull items out of the database perfectly happily but when I edit them and try to save them it throws the following exception:
>>> p = Problem.query.first()
>>> p
<Problem('Test Problem', 'This is a test problem so the database has something in it', 'SBMT')>
>>> p.name
u'Test Problem'
>>> p.name = "Test_Problem"
>>> p.save()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/site-packages/elixir/entity.py", line 1116, in save
return self._global_session.save(self, *args, **kwargs)
AttributeError: 'Session' object has no attribute 'save'
What am I doing wrong? Have I missed a crucial part of the set up that is preventing me from saving things to the database or is it a problem with my versions of elixir and SQLAlchemy?
I've already run setup_all()
and the metadata.bind
is all set, hence I can query the database.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不是 Elixir 专家,但从文档来看,它似乎使用了一种称为全局会话的东西。
要将更改保存到数据库,您需要执行
session.commit()
,没有像 Django 中那样的save()
方法。I'm no Elixir expert, but from the documentation it looks like it uses something called a global session.
To save the changes to the database, you do a
session.commit()
, there's nosave()
method like in Django.