![](/img/trans.png)
[英]Postgresql: update data from dataframe python in spesific column table
[英]Cannot access column in PostgreSQL table from Python model
我定義了一個python類“ Students”,如下所示:
class Students(DeclarativeBase):
__tablename__ = 'students'
id_ = Column('id', Integer, primary_key=True)
name = Column('nombre', Unicode(50))
date_of_birth = Column(Date)
如果我確實select * from students
,則可以看到所有這些列以及其他一些列,即_created
和_updated
。
我需要使用存儲在_created
和_updated
列中的值。 所以我嘗試像這樣訪問它們:
#get student with id = 1
>>> s = dbs.query(Students).get(1)
# print its name
>>> print(s.name)
Richard
# try to print when it was created
>>> print (s._created)
AttributeError: 'Students' object has no attribute '_created'
我當然會收到該消息,因為模型中未定義_created
屬性。
即使它不是Student
類的屬性,我如何也可以訪問存儲在Students
表中的值?
SQLAlchemy需要定義將訪問的每個列。 (有幾種方法可以通過反映數據庫來自動發現,但顯式要比隱式好 。)將列定義添加到模型中。 我假設它們是DateTime
。 插入或更新行時,可以使用default=
和onupdate=
提供新值。
class Student(Base):
__tablename__ = 'student'
id = Column('id_', Integer, primary_key=True)
# other columns...
created = Column('_created', DateTime, nullable=False, default=datetime.utcnow)
updated = Column('_updated', DateTime, onupdate=datetime.utcnow)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.