postgresql SQLAlchemy 会话 add() 返回值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19388555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:09:07  来源:igfitidea点击:

SQLAlchemy Session add() return value

pythonpostgresqlsqlalchemy

提问by Pravin Reddy

working on pyramid with sqlalchemy:

使用 sqlalchemy 处理金字塔:

newjob_obj=Job(name=name,job_propery=job_property,sizeX=sizeX,
    sizeY=sizeY,quantity=quantity,timeline=timeline,
    description=description,remarks=remarks,
    client_id=client_id,created_by=created_by,status=status
)
new_job=session.add(newjob_obj)
print('Return newJob value %s\n' % new_job)

Here new_job is printing as None. add function of session returns object or not. Please help.

这里 new_job 打印为None. 会话的添加函数是否返回对象。请帮忙。

回答by Ricky Levi

To answer your question in the comment of @mark's answer - in order to receive your "inserted ID" after the commit:

要在@mark 的答案的评论中回答您的问题 - 以便在提交后收到您的“插入的 ID”:

session.add(newjob_obj)
session.commit()

You should refresh the inserted object with:

您应该使用以下内容刷新插入的对象:

session.refresh(newjob_obj)
print newjob_obj.id

Hope it helps ..

希望能帮助到你 ..

回答by Mark Hildreth

This is the expected output. add()does not return a value. The documentation:

这是预期的输出。add()不返回值。文档

Place an object in the Session.

Its state will be persisted to the database on the next flush operation.

Repeated calls to add() will be ignored. The opposite of add() is expunge().

在会话中放置一个对象。

在下一次刷新操作时,它的状态将被持久化到数据库中。

重复调用 add() 将被忽略。add() 的反面是 expunge()。

The code:

代码

def add(self, instance, _warn=True):
    """Place an object in the ``Session``.

    Its state will be persisted to the database on the next flush
    operation.

    Repeated calls to ``add()`` will be ignored. The opposite of ``add()``
    is ``expunge()``.

    """
    if _warn and self._warn_on_events:
        self._flush_warning("Session.add()")

    try:
        state = attributes.instance_state(instance)
    except exc.NO_STATE:
        raise exc.UnmappedInstanceError(instance)

    self._save_or_update_state(state)

The add method does not return a value. When a Python function does not return a value, the function acts as if it returns None. If you wanted to print out the job, you would instead print:

add 方法不返回值。当 Python 函数不返回值时,该函数就像返回None. 如果你想打印出作业,你应该打印:

session.add(newjob_obj)
print('Return newJob value %s\n' % newjob_obj)

You see, SQLAlchemy will not really do anything important (like run a query against a database) when you add()an object to the session. What it will do is just keep track of the fact that the object exists. Then when you do a...

您会看到,当您add()将对象添加到会话时,SQLAlchemy 不会真正做任何重要的事情(例如对数据库运行查询)。它会做的只是跟踪对象存在的事实。那么当你做...

session.commit()

...all of the objects that you added are INSERTed into the database (among other things, like doing UPDATE and DELETE for modified and removed objects).

...您添加的所有对象都被插入到数据库中(其中包括对修改和删除的对象执行 UPDATE 和 DELETE)。

See the using the session chapterin he documentation for more info.

有关更多信息,请参阅文档中的使用会话章节