question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Aggregating by month: "user-defined function raised exception"

See original GitHub issue

Hi, I’m trying to extract month from date column in my database. Just as it’s shown here.

Here is my code:

 query = (dc.Message.select(dc.Message.date.month.alias('month')).limit(5))
    
    for q in query:
        print(q.month)

And Message model:

class Message(BaseModel):
    text = CharField(null = True)
    created_by = ForeignKeyField(User)
    posted_in = ForeignKeyField(Chat)
    date = DateTimeField()

When I try to execute my code, I get this message in debug console:

peewee.OperationalError: user-defined function raised exception

Do you have any ideas how to fix that?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
coleifercommented, Jan 30, 2018

The problem is that the datetime strings contain timezone information, which is not supported by these methods. If using SQLite, which does not contain a native datetime type, dates are stored in a text column in a format that ensures lexicographic ordering (so sorting and comparing datetimes works as you’d expect). You have the format correct (Y-m-d H:M:S) but the inclusion of the timezone will make ordering not work correctly (unless all values have the same timezone, which, if the case, means you can probably do without storing it anyways?). So to use the helper functions with SQLite, you’ll want to store all datetimes as naive datetimes. If you use timezones, just convert to UTC before storing and convert back to local-time before using them in your application.

0reactions
coleifercommented, Jan 30, 2018

You should be able to cast as an integer. Sqlite Cast docs:

MyModel.select().order_by(MyModel.timestamp.month.cast('INTEGER'))
Read more comments on GitHub >

github_iconTop Results From Across the Web

user-defined function raised exception occurred - Stack Overflow
Show activity on this post. I have following error when I'm using below user defined function with raw query: Django Version: ...
Read more >
Compute Functions — Apache Arrow v10.0.1
The grouped aggregation functions raise an exception instead and need to be used ... PyArrow allows defining and registering custom compute functions.
Read more >
Built-in functions | Databricks on AWS
Learn about built-in functions in Databricks SQL and Databricks Runtime. ... Aggregates elements in an array using a custom aggregator.
Read more >
when other than exception, and raise application error
UTL_FILE raises about 5 or 6 different "user defined exceptions" -- i use raise_application_error ... COM> show err Errors for FUNCTION FOO: LINE/COL...
Read more >
Query Expressions - Django documentation
All of the aggregate functions, like Sum() and Count() , inherit from ... window functions in the WHERE clause and Django raises an...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found