OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Snowflake SQLAlchemy - Dynamically created column with Timestamp?

  • Thread starter Thread starter MountainBiker
  • Start date Start date
M

MountainBiker

Guest
This is a follow-up question to my previous one.

Snowflake SQLAlchemy - Create table with Timestamp?

I am dynamically creating columns and I have this schema.

I need the "my_time_stamp" column to have the server default.

Code:
json_cls_schema = {
    "clsname": "MyClass",
    "tablename": "my_table",
    "columns": [
        {"name": "id", "type": "integer", "is_pk": True, "is_auto" : True},
        {"name": "my_time_stamp", "type": "timestamp", 'serverdefault': text('current_timestamp')}
    ],
}

How would I set so that the code below generates this column.

Code:
my_time_stamp = Column(TIMESTAMP,server_default=text('current_timestamp()'))

Right now I am getting the error TypeError: Object of type TextClause is not JSON serializable and the dictionary I am generating this column from looks like this.

Code:
{'name': 'my_time_stamp', 'type': 'timestamp', 'serverdefault': <sqlalchemy.sql.elements.TextClause object at 0x000001B57664F560>}

code:

Code:
_type_lookup = {
    "integer": Integer,
    "timestamp": TIMESTAMP,


}

def mapping_for_json(json_cls_schema):
    clsdict = {"__tablename__": json_cls_schema["tablename"]}

    clsdict.update(
        {
            rec["name"]:
                Column(
                    _type_lookup[rec["type"]], primary_key=rec.get("is_pk", False),
                    autoincrement=rec.get("is_auto", False), server_default=rec.get("serverdefault", '')
                )
            for rec in json_cls_schema["columns"]
        }
    )


    return type(json_cls_schema["clsname"], (Base,), clsdict)
<p>This is a follow-up question to my previous one.</p>
<p><a href="https://stackoverflow.com/questions...create-table-with-timestamp/78623238#78623238">Snowflake SQLAlchemy - Create table with Timestamp?</a></p>
<p>I am dynamically creating columns and I have this schema.</p>
<p>I need the "my_time_stamp" column to have the server default.</p>
<pre><code>json_cls_schema = {
"clsname": "MyClass",
"tablename": "my_table",
"columns": [
{"name": "id", "type": "integer", "is_pk": True, "is_auto" : True},
{"name": "my_time_stamp", "type": "timestamp", 'serverdefault': text('current_timestamp')}
],
}
</code></pre>
<p>How would I set so that the code below generates this column.</p>
<pre><code>my_time_stamp = Column(TIMESTAMP,server_default=text('current_timestamp()'))
</code></pre>
<p>Right now I am getting the error TypeError: Object of type TextClause is not JSON serializable and the dictionary I am generating this column from looks like this.</p>
<pre><code>{'name': 'my_time_stamp', 'type': 'timestamp', 'serverdefault': <sqlalchemy.sql.elements.TextClause object at 0x000001B57664F560>}
</code></pre>
<p>code:</p>
<pre><code>_type_lookup = {
"integer": Integer,
"timestamp": TIMESTAMP,


}

def mapping_for_json(json_cls_schema):
clsdict = {"__tablename__": json_cls_schema["tablename"]}

clsdict.update(
{
rec["name"]:
Column(
_type_lookup[rec["type"]], primary_key=rec.get("is_pk", False),
autoincrement=rec.get("is_auto", False), server_default=rec.get("serverdefault", '')
)
for rec in json_cls_schema["columns"]
}
)


return type(json_cls_schema["clsname"], (Base,), clsdict)
</code></pre>
 

Latest posts

D
Replies
0
Views
1
Dhanushka Amarakoon
D
S
Replies
0
Views
1
Shikhar Ambashta
S
Top