設定:Postgres 13、Python 3.7、SQLAlchemy 1.4
當前結構用于base.py創建引擎、Scoped Session 和 Augmented Base。所有這些都在models.py我們定義表類的地方被呼叫,并inserts.py在我們使用 ORM 測驗向資料庫插入新值的地方再次被呼叫。所有這些都運作良好。
我的問題是關于表類中的def __init__(self)函式models.py。如果沒有這個功能,代碼會出錯TypeError: __init__() takes 1 positional argument but 5 were given
一旦我包含該__init__功能,代碼就可以正常作業。
鑒于所有模型都是通過宣告式系統定義的,這意味著我們的類應該獲得一個__init__()方法建構式,該方法建構式自動接受與我們映射的列匹配的關鍵字名稱,因此我對為什么會產生此錯誤感到困惑。
我懷疑我如何編碼 和 之間的互動db_session = scoped_session以及
傳入Base = declarative_base(cls=Base, metadata=metadata_obj)的方式存在錯誤。Baseclass NumLimit(Base)
我不能完全解決這個問題,希望能被引導到我創建這個錯誤的地方。謝謝!
基礎檔案
from sqlalchemy import Column, create_engine, Integer, MetaData
from sqlalchemy.orm import declared_attr, declarative_base, scoped_session, sessionmaker
engine = create_engine('postgresql://user:pass@localhost:5432/dev', echo=True)
db_session = scoped_session(
sessionmaker(
bind=engine,
autocommit=False,
autoflush=False
)
)
# Augment the base class by using the cls argument of the declarative_base() function so all classes derived
# from Base will have a table name derived from the class name and an id primary key column.
class Base:
@declared_attr
def __tablename__(cls):
return cls.__name__.lower()
id = Column(Integer, primary_key=True)
# Write all tables to schema 'collect'
metadata_obj = MetaData(schema='collect')
# Instantiate a Base class for our classes definitions
Base = declarative_base(cls=Base, metadata=metadata_obj)
模型.py
from base import Base
from sqlalchemy import Column, DateTime, Integer, Text
from sqlalchemy.dialects.postgresql import UUID
import uuid
class NumLimit(Base):
org = Column(UUID(as_uuid=True), default=uuid.uuid4, unique=True)
limits = Column(Integer)
limits_rate = Column(Integer)
rate_use = Column(Integer)
def __init__(self, org, limits, allowance_rate, usage, last_usage):
super().__init__()
self.org = org
self.limits = limits
self.limits_rate = limits_rate
self.rate_use = rate_use
def __repr__(self):
return f'<NumLimit(org={self.org}, limits={self.limits}, limits_rate={self.limits_rate},' \
f' rate_use={self.rate_use})>'
插入.py
def insert_num_limit():
# Generate database schema based on definitions in models.py
Base.metadata.create_all(bind=engine)
# Create instances of the NumLimit class
a_num_limit = NumLimit('123e4567-e89b-12d3-a456-426614174000', 20, 4, 8)
another_limit = NumLimit('123e4567-e89b-12d3-a456-426614174660', 7, 2, 99)
# Use the current session to persist data
db_session.add_all([a_num_limit, another_limit])
# Commit current session to database and close session
db_session.commit()
db_session.close()
return
uj5u.com熱心網友回復:
__init__()sqlalchemy 生成的所有引數都是keyword-only。就好像定義是:
def __init__(self, *, id=None, org=None, limits=None, limits_rate=None, rate_use=None):
self.id = id
self.org = org
# etc...
因此,當您嘗試按位置提供引數時:NumLimit('123e4567-e89b-12d3-a456-426614174000', 20, 4, 8),您將收到錯誤TypeError: __init__() takes 1 positional argument but 5 were given,因為__init__()實際上只需要一個位置引數。但是,如果您將它們作為關鍵字引數提供:NumLimit(id='123e4567-e89b-12d3-a456-426614174000', org=20, limits=4, limits_rate=8)一切正常。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/348648.html
標籤:Python PostgreSQL sqlalchemy 奥姆 在里面
上一篇:檢查列資料型別postgresql時,我得到了多個結果
下一篇:過濾掉相似值字串sql
