Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5968017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:01:44+00:00 2026-05-22T20:01:44+00:00

I am working with an SQLAlchemy database. I have a simple schema with columns

  • 0

I am working with an SQLAlchemy database.

I have a simple schema with columns id, data, and timestamp. The timestamp column is automatically populated with the current date/time as follows:

Column('timestamp', DateTime, nullable=False, default=datetime.now)

I want to add another column containing the SHA-1 checksum of the data column. Something along the lines of:

# notice this is INVALID CODE
Column('checksum', String, nullable=False, unique=True,
       default=hashlib.sha1(this_table.data).hexdigest())

Any suggestions?

Thank you.

Edit:

The closest I have come to this is to manage the “automation” at the object level (as opposed to the table level). I simply define the checksum column as

Column('checksum', String, nullable=False, unique=True)

and modify the constructor of the object mapped to that table as:

def __init__(self, data):
 self.data = data
 self.checksum = hashlib.sha1(self.data).hexdigest()

It works as expected, but I still wonder if there is a way to do this at the “table” level (as in the timestamp, where I do nothing at the object level, but correctly assign the current date/time)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T20:01:45+00:00Added an answer on May 22, 2026 at 8:01 pm

    The SqlAlchemy MapperExtension allows you to create code-side triggers/hooks for events.

    http://www.sqlalchemy.org/docs/06/orm/interfaces.html?

    Basically you’d want to create a before_insert and before_update that performs the extra actions. I’ve got an example using this to help make sure I copy content from one column into a fulltext indexed table elsewhere in the database:

    init.py#L269″>https://github.com/mitechie/Bookie/blob/master/bookie/models/init.py#L269

    So your mapper extension might be something like

    class DataSHAExtension(MapperExtension):
        def before_insert(self, mapper, connection, instance):
            instance.checksum = hashlib.sha1(instance.data).hexdigest()
        ...
    

    and then attched to the relevent model:

    Class Something(Base):
        __tablename__ = 'something'
        __mapper_args__ = {
            'extension': DataSHAExtension()
        }
    

    This is doing things the declarative way. You can also do this with the manual mapper commands.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got a simple problem in SQLAlchemy. I have one model in a table,
I am working with SQLAlchemy, and I'm not yet sure which database I'll use
Working on a project at the moment and we have to implement soft deletion
Working with an Oracle 9i database from an ASP.NET 2.0 (VB) application using OLEDB.
I have been working on a website using mod_python, python, and SQL Alchemy when
I am working on my first pylons + SQLAlchemy app (I'm new to both).
I am working on a web app based on Flask and SQLAlchemy. Most of
Working with Visual Studio (I'm using 2008) I have started to notice that when
I'm working on my first Pylons project using SQLalchemy and sqlite. I'd like to
I'm currently working on a proof of concept application using Python 3.2 via SQLAlchemy

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.