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 8829753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:54:25+00:00 2026-06-14T07:54:25+00:00

With SQLAlchemy, I’m finding that sometimes I mis-type a name of an attribute which

  • 0

With SQLAlchemy, I’m finding that sometimes I mis-type a name of an attribute which is mapped to a column, which results in rather difficult to catch errors:

class Thing(Base):
    foo = Column(String)


thing = Thing()
thing.bar = "Hello" # a typo, I actually meant thing.foo
assert thing.bar == "Hello" # works here, as thing.bar is a transient attribute created by the assignment above
session.add(thing)
session.commit() # thing.bar is not saved in the database, obviously
...
# much later
thing = session.query(Thing)...one()
assert thing.foo == "Hello" # fails
assert thing.bar == "Hello" # fails, there's no even such attribute

Is there a way to configure the mapped class so assigning to anything which is not mapped to an SQLAlchemy column would raise an exception?

  • 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-06-14T07:54:26+00:00Added an answer on June 14, 2026 at 7:54 am

    Ok, the solution seems to be to override __setattr__ method of the base class, which allows us to check if the atribute already exists before setting it.

    class BaseBase(object):
        """
        This class is a superclass of SA-generated Base class,
        which in turn is the superclass of all db-aware classes
        so we can define common functions here
        """
    
        def __setattr__(self, name, value):
            """
            Raise an exception if attempting to assign to an atribute which does not exist in the model.
            We're not checking if the attribute is an SQLAlchemy-mapped column because we also want it to work with properties etc.
            See http://stackoverflow.com/questions/12032260/ for more details.
            """ 
            if name != "_sa_instance_state" and not hasattr(self, name):
                raise AttributeError("Attribute %s is not a mapped column of object %s" % (name, self))
            super(BaseBase, self).__setattr__(name, value)
    
    Base = declarative_base(cls=BaseBase)
    

    Sort of “strict mode” for SQLAlchemy…

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

Sidebar

Related Questions

I have table in sqlalchemy 0.4 that with types.DateTime column: Column(dfield, types.DateTime, index=True) I
Given a SQLAlchemy mapped class Table and an instance of that class t ,
What is the sqlalchemy equivalent column type for 'money' and 'OID' column types in
I have an SQLAlchemy scheme that looks roughly like this: participation = db.Table('participation', db.Column('artist_id',
I am working with SQLAlchemy, and I'm not yet sure which database I'll use
With sqlalchemy, if one has an object with a orm relation attribute, and you
I have an SQLAlchemy ORM class, linked to MySQL, which works great at saving
SqlAlchemy newbie question: Base = declarative_base() class A(Base): __tablename__ = 'as' id = Column(Integer,
In SQLAlchemy , a hybrid attribute is either a property or method applied to
I've got a sqlalchemy model that is set up like this: class Entry(Base): __tablename__

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.