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

  • SEARCH
  • Home
  • 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 869931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:23:15+00:00 2026-05-15T10:23:15+00:00

I like the idea of sqlite’s manifest typing / type affinity: http://www.sqlite.org/datatype3.html Essentially, if

  • 0

I like the idea of sqlite’s manifest typing / type affinity:

http://www.sqlite.org/datatype3.html

Essentially, if I set a column’s affinity as ‘numeric’, it will duck type integers or floats to store them as such, but still allow me to store strings if I want to. Seems to me this is the best ‘default’ type for a column when i’m not sure ahead of time of what data i want to store in it.

so off i go:

metadata = MetaData()
new_table = Table(table_name, metadata )
for col_name in column_headings:
    new_table.append_column(Column(col_name, 
                                   sqlite.NUMERIC, #this should duck-type numbers but can handle strings as well
                                   primary_key=col_name in primary_key_columns))
new_table.create(self.engine, checkfirst=False)

but when i try and store some string values, eg “abc” in the table, sqlalchemy falls over:

  File "[...]\sqlalchemy\processors.py", line 79, in to_float
    return float(value)
ValueError: invalid literal for float(): abc

Boo, hiss. So, is there any way I can convince sqlalchemy to let sqlite do the typing? perhaps i can use a type from sqlalchemy.types instead of sqlachemy.dialects.sqlite?

[edit:] for bonus points: i need to be able to access tables via introspection / reflection. so some kind of way of having this work with meta.reflect() would be great! 😉

  • 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-15T10:23:15+00:00Added an answer on May 15, 2026 at 10:23 am

    OK, here’s what I’ve come up with:

    Define a custom column type, as per
    http://www.sqlalchemy.org/docs/reference/sqlalchemy/types.html#custom-types

    a combination of the documentation and some trial & error have given me this:

    class MyDuckType(sqlalchemy.types.TypeDecorator):
        """
        SQLALchemy custom column type, designed to let sqlite handle the typing 
        using 'numeric affinity' which intelligently handles both numbers and strings
        """
        impl = sqlite.NUMERIC
    
        def bind_processor(self, dialect):
            #function for type coercion during db write
            return None #ie pass value as-is, let sqlite do the typing
    
        def result_processor(self, dialect, coltype):
            #function for type coercion during db read
            return None #ie pass value as sqlite has stored it, should be ducktyped already
    
        def process_bind_param(self, value, dialect):
            #any changes to an individual value before store in DN
            return value
    
        def process_result_value(self, value, dialect):
            #any changes to an individual value after retrieve from DB
            return value
    
        def copy(self):
            #not quite sure what this is for
            return MyDuckType()
    

    The current sqlalchemy dialect type returns to_float in bind_processor, which is why I was getting the errors before. i.m.v.v.h.o., this is a bug.

    for my bonus points: manually setting column type to MyDuckType in my metadata.reflect() code:

    def get_database_tables(engine):
        meta = MetaData()
        meta.reflect(bind=engine)
        tables = meta.raw_tables
        for tbl in tables.values():
            for col in tbl.c:
                col.type = MyDuckType()
        return tables
    

    seems to work for me. Any suggestions / improvements?

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

Sidebar

Related Questions

I followed this link http://www.phpunit.de/manual/3.6/en/database.html there i found some useful details of php db
I like the idea of using an In-Memory Database such as SQLite when testing
I like the idea of command prompt coloring, and I was wondering how I
I like the idea of abstracting CellControllers in a heterogeneous UITableView as suggested by
I really like the idea of automatic binding generation like SWIG does. But it
I'm working on a REST API and I really like the idea of using
Hi I'm new to Linux and I really like the idea of writing and
I've been using virtualenv lately while developing in python. I like the idea of
My idea is like if there is a exist file , add the filename
sound like a terrible idea? Yeah I thought so too. Needless to say im

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.