I've a csv file on an ftp, and want to store the timestamp and the number of lines (of this file) to the database. Unfortunately the datetime it is always set to NULL.
...
class Foo(Base):
__tablename__ = 'Foo'
lines = Column("lines", Integer, primary_key=True)
timestamp_of_csv_file = Column("timestamp_of_csv_file", DateTime)
def __init__(self, lines, timestamp):
self.lines = lines
self.timestamp = timestamp
remote_file = sftp.open(remote_file_name)
file_date = sftp.stat(remote_file_name).st_mtime
dt_m = datetime.fromtimestamp(file_date)
data = remote_file.read()
count_lines = data.count(b'\r\n')
session.add(Foo(dt_m, count_lines))
session.commit()
This is the corresponding entry in the postgres log:
postgres@my_db(0)2023-06-27 09:24:11 CEST LOG: statement: INSERT INTO "Foo" (timestamp_of_csv_file, lines) VALUES NULL, 12329)
Breakpoint in the "init":
-> self.timestamp = timestamp
(Pdb) timestamp
datetime.datetime(2023, 6, 26, 20, 35, 14)
What am I doing wrong?
You need to modify the constructor to assign the timestamp paramater to the right column name
class Foo(Base):
__tablename__ = 'Foo'
lines = Column("lines", Integer, primary_key=True)
timestamp_of_csv_file = Column("timestamp_of_csv_file", DateTime)
def __init__(self, lines, timestamp):
self.lines = lines
self.timestamp_of_csv_file = timestamp