Skip to content

Commit 65fa074

Browse files
adding example table-with-default-timestamp.py
thanks to @sfc-gh-mraba
1 parent 58fb1bd commit 65fa074

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# courtesy of sfc-gh-mraba
2+
from sqlalchemy import (
3+
Column,
4+
Integer,
5+
String,
6+
func,
7+
)
8+
from sqlalchemy.orm import Session, declarative_base
9+
from snowflake.sqlalchemy import TIMESTAMP_NTZ
10+
11+
Base = declarative_base()
12+
13+
class TWTS(Base):
14+
__tablename__ = "table_with_timestamp"
15+
16+
pk = Column(Integer, primary_key=True)
17+
name = Column(String(30))
18+
created = Column(TIMESTAMP_NTZ, server_default=func.now())
19+
20+
def __repr__(self) -> str:
21+
return f"TWTS({self.pk=}, {self.name=}, {self.created=})"
22+
23+
Base.metadata.create_all(engine_testaccount)
24+
25+
session = Session(bind=engine_testaccount)
26+
r1 = TWTS(pk=1, name="edward")
27+
r2 = TWTS(pk=2, name="eddy")
28+
assert r1.created is None
29+
assert r2.created is None
30+
31+
session.add(r1)
32+
session.add(r2)
33+
session.commit()
34+
35+
rows = session.query(TWTS).all()
36+
assert len(rows) == 2
37+
for row in rows:
38+
print(row)

0 commit comments

Comments
 (0)