Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Group By failed with date_trunc #148

Open
rcourivaud opened this issue Jan 11, 2024 · 3 comments
Open

Group By failed with date_trunc #148

rcourivaud opened this issue Jan 11, 2024 · 3 comments

Comments

@rcourivaud
Copy link

I don't really understand but this query runs well with pg8000 driver 1.30.3 but fails with the new version

db.query(
Invoice.energy,
func.date_trunc("month", Invoice.date).label("month"),
func.sum(Invoice.consumption).label("consumption"),
)

query = query.group_by(Invoice.energy, func.date_trunc("month", Invoice.date))

@tlocke
Copy link
Owner

tlocke commented Jan 11, 2024

Hi @rcourivaud thanks for your bug report. I think it would be easier to fix the bug if you could provide a Minimal Reproducible Example.

@rcourivaud
Copy link
Author

rcourivaud commented Jan 12, 2024

Finally, after few tests, the error is also on the older versions. But it works fine with other postgres drivers.

For the example :

def local_connector():
    POSTGRES_USER = os.environ.get("POSTGRES_USER")
    POSTGRES_PASSWORD = os.environ.get("POSTGRES_PASSWORD")
    POSTGRES_DB = os.environ.get("POSTGRES_DB")
    POSTGRES_HOST = os.environ.get("POSTGRES_HOST")
    POSTGRES_PORT = os.environ.get("POSTGRES_PORT", "5432")

    SQLALCHEMY_DATABASE_URL = f"postgresql+pg8000://{POSTGRES_USER}:{POSTGRES_PASSWORD}@{POSTGRES_HOST}:{POSTGRES_PORT}/{POSTGRES_DB}"
    print(SQLALCHEMY_DATABASE_URL)

    engine = create_engine(SQLALCHEMY_DATABASE_URL)
    return engine
    
engine = local_connector()
SessionLocal = sessionmaker(autocommit=False, autoflush=True, bind=engine)
    query = (
        db.query(
            Invoice.energy,
            func.date_trunc("month", Invoice.date).label("month"),
            func.sum(Invoice.consumption).label("consumption"),
        )
    )
    query = query.group_by(Invoice.energy, func.date_trunc("month", Invoice.date))


    query.all()
     result_dict = defaultdict(list)    

You can test with this file
invoices - Feuille 1.csv

@tlocke
Copy link
Owner

tlocke commented Jan 12, 2024

Hi @rcourivaud, that's closer to a Minimal Reproducible Example, but not quite there yet. For instance a MinRep should be all in one file. Also, I can't see any code to set up the table? The idea is to produce a script that anyone can run that reproduces the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants