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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:41:39+00:00 2026-06-17T23:41:39+00:00

I have a table that contains a column called region_code with Integer as its

  • 0

I have a table that contains a column called region_code with Integer as its datatype. In my Python program I have this line:

region = session.query(Region).filter(Region.region_code/100 == region_prefix).one()

The important part here is the chosen filter method

Region.region_code/100 == region_prefix

where region_code is the integer column in the table and the region_prefix variable holds an integer value

The region_code is the integer column in the table containing values like 1199, 1200, 1300, 1499 while the region_prefix variable (from the program) holds integer values like 11, 12, 13, 14.

What I now want to achive with this filter option is to perform an integer division on region_code (hence cut everything behind the decimal place) to be able to compare it with region_prefix.

Example:

region_prefix = 11

Assume that in the Region table exists a entry with

region_code = 1199

The calculation within the filter should look like this

1199 / 100 = 11

So with this result I am able to filter for region_prefix (11=11).
This works just fine with MSSQL (Microsoft).

Unfortunately (in my case) MySQL division operator / does not do integer divisions. So the result of 1199/100 will be 11.99 and so my query will not find a fitting entry in the DB (because 11.99 != 11).
Nevertheless MySQL actually has the inter division implemented: the DIV operator.

So the following SQL query works just fine for me:

SELECT * FROM Region where region_code DIV 100 = 11;

So my question is:
How do I get the SQLAlchemy filter method to use DIV instead of / operator? Is this possible? Or is the only workaround to use native queries?

  • 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-17T23:41:40+00:00Added an answer on June 17, 2026 at 11:41 pm

    Luckily SQLAlchemy allows for custom operators. There are simple ways of doing this (simply specifying in the query) or more complex versions of doing it (fully defining the new operator and making it multi database aware). So it depends on what you’re looking for.

    http://docs.sqlalchemy.org/en/latest/core/custom_types.html?highlight=operators#redefining-and-creating-new-operators

    region = (session
        .query(Region)
        .filter(Region.region_code.op('div')(region_prefix))
        .one()
    )
    

    And the fancy way:

    http://docs.sqlalchemy.org/en/rel_0_8/core/types.html#types-operators

    from sqlalchemy import Integer
    
    class DivisibleInteger(Integer):
        class comparator_factory(Integer.Comparator):
            def div(self, other):
                return self.op('div')(other)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that contains a column called 'id' that is an INT
I have a table that contains a VARCHAR2 column called COMMANDS. The data in
I have a table (lets say it has one column called 'colLanguage') that contains
I have a table that contains a column of XML Datatype (column name FileContent).
I have a table called Order_List that contains a list of orders. Each column
My database has a products table. This contains a column called layers . That
I have a table called users which currently contains column money of type integer
You have a table table1 that contains id column, that is int(11), not null,
I have a table that contains an Id column as a primary key. There
I have a table that contains records with a column indicates the Date. Given

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.