gpt4 book ai didi

python - 如何在 sqlalchemy 中为我的主键列配置序列对象?

转载 作者:行者123 更新时间:2023-12-01 06:51:09 24 4
gpt4 key购买 nike

我希望 MediaFolder 类在添加到数据库或初始化时自动生成 id。我试过:

from sqlalchemy import Column, String, BigInteger, ForeignKey, Sequence
from __init__ import Base


class MediaFolder(Base):
__tablename__ = 'media_folder'

seq = Sequence('id', start=20000000)
id = Column(BigInteger, server_default=seq.next_value(), primary_key=True)

name = Column(String, nullable=False)

username = Column(String, nullable=False)

x = MediaFolder()
x.name = 'test seq'
x.username = 'test seq u'


from __init__ import db_session
db_session.add(x)
db_session.commit()

但是当执行时,我得到:

Traceback (most recent call last):
File "C:\Users\Krzysiek\Anaconda3\lib\site-packages\sqlalchemy\engine\base.py", line 1249, in _execute_context
cursor, statement, parameters, context
File "C:\Users\Krzysiek\Anaconda3\lib\site-packages\sqlalchemy\engine\default.py", line 580, in do_execute
cursor.execute(statement, parameters)
psycopg2.errors.NotNullViolation: null value in column "id" violates not-null constraint
DETAIL: Failing row contains (null, test seq, test seq u, null).


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "C:/Users/Krzysiek/Desktop/praca/media_library/media/media_folder.py", line 29, in <module>
db_session.commit()
File "C:\Users\Krzysiek\Anaconda3\lib\site-packages\sqlalchemy\orm\scoping.py", line 162, in do
return getattr(self.registry(), name)(*args, **kwargs)
File "C:\Users\Krzysiek\Anaconda3\lib\site-packages\sqlalchemy\orm\session.py", line 1027, in commit
self.transaction.commit()
File "C:\Users\Krzysiek\Anaconda3\lib\site-packages\sqlalchemy\orm\session.py", line 494, in commit
self._prepare_impl()
File "C:\Users\Krzysiek\Anaconda3\lib\site-packages\sqlalchemy\orm\session.py", line 473, in _prepare_impl
self.session.flush()
File "C:\Users\Krzysiek\Anaconda3\lib\site-packages\sqlalchemy\orm\session.py", line 2470, in flush
self._flush(objects)
File "C:\Users\Krzysiek\Anaconda3\lib\site-packages\sqlalchemy\orm\session.py", line 2608, in _flush
transaction.rollback(_capture_exception=True)
File "C:\Users\Krzysiek\Anaconda3\lib\site-packages\sqlalchemy\util\langhelpers.py", line 68, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "C:\Users\Krzysiek\Anaconda3\lib\site-packages\sqlalchemy\util\compat.py", line 153, in reraise
raise value
File "C:\Users\Krzysiek\Anaconda3\lib\site-packages\sqlalchemy\orm\session.py", line 2568, in _flush
flush_context.execute()
File "C:\Users\Krzysiek\Anaconda3\lib\site-packages\sqlalchemy\orm\unitofwork.py", line 422, in execute
rec.execute(self)
File "C:\Users\Krzysiek\Anaconda3\lib\site-packages\sqlalchemy\orm\unitofwork.py", line 589, in execute
uow,
File "C:\Users\Krzysiek\Anaconda3\lib\site-packages\sqlalchemy\orm\persistence.py", line 245, in save_obj
insert,
File "C:\Users\Krzysiek\Anaconda3\lib\site-packages\sqlalchemy\orm\persistence.py", line 1137, in _emit_insert_statements
statement, params
File "C:\Users\Krzysiek\Anaconda3\lib\site-packages\sqlalchemy\engine\base.py", line 988, in execute
return meth(self, multiparams, params)
File "C:\Users\Krzysiek\Anaconda3\lib\site-packages\sqlalchemy\sql\elements.py", line 287, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "C:\Users\Krzysiek\Anaconda3\lib\site-packages\sqlalchemy\engine\base.py", line 1107, in _execute_clauseelement
distilled_params,
File "C:\Users\Krzysiek\Anaconda3\lib\site-packages\sqlalchemy\engine\base.py", line 1253, in _execute_context
e, statement, parameters, cursor, context
File "C:\Users\Krzysiek\Anaconda3\lib\site-packages\sqlalchemy\engine\base.py", line 1473, in _handle_dbapi_exception
util.raise_from_cause(sqlalchemy_exception, exc_info)
File "C:\Users\Krzysiek\Anaconda3\lib\site-packages\sqlalchemy\util\compat.py", line 398, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "C:\Users\Krzysiek\Anaconda3\lib\site-packages\sqlalchemy\util\compat.py", line 152, in reraise
raise value.with_traceback(tb)
File "C:\Users\Krzysiek\Anaconda3\lib\site-packages\sqlalchemy\engine\base.py", line 1249, in _execute_context
cursor, statement, parameters, context
File "C:\Users\Krzysiek\Anaconda3\lib\site-packages\sqlalchemy\engine\default.py", line 580, in do_execute
cursor.execute(statement, parameters)
sqlalchemy.exc.IntegrityError: (psycopg2.errors.NotNullViolation) null value in column "id" violates not-null constraint
DETAIL: Failing row contains (null, test seq, test seq u, null).

[SQL: INSERT INTO media_folder (name, username) VALUES (%(name)s, %(username)s) RETURNING media_folder.id]
[parameters: {'name': 'test seq', 'username': 'test seq u'}]
(Background on this error at: http://sqlalche.me/e/gkpj)

基本上id返回null。我已尽一切努力来解决这个问题或理解这个顺序。如何正确生成 id 值?或者也许还有另一种方法可以在 sqlalchemy 中自动生成值?

最佳答案

您可以创建序列作为 id 列声明的一部分。

class MediaFolder(Base):
__tablename__ = 'media_folder'

seq = Sequence('id', start=20000000)
id = Column(BigInteger, Sequence('media_folder_id', start=20000000), primary_key=True)

name = Column(String, nullable=False)
username = Column(String, nullable=False)

这是代码执行后数据库中的内容:

test=# \d media_folder_id
Sequence "public.media_folder_id"
Type | Start | Minimum | Maximum | Increment | Cycles? | Cache
--------+----------+---------+---------------------+-----------+---------+-------
bigint | 20000000 | 1 | 9223372036854775807 | 1 | no | 1

test=# SELECT * FROM media_folder;
id | name | username
----------+----------+------------
20000000 | test seq | test seq u

(为序列指定一个比“id”更独特的名称是个好主意)。

关于python - 如何在 sqlalchemy 中为我的主键列配置序列对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58998855/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com