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

[Bug report] When using MariaDB, add generates wrong SQL #494

Closed
tebrown opened this issue Feb 29, 2024 · 9 comments · Fixed by #495
Closed

[Bug report] When using MariaDB, add generates wrong SQL #494

tebrown opened this issue Feb 29, 2024 · 9 comments · Fixed by #495
Assignees
Labels

Comments

@tebrown
Copy link
Contributor

tebrown commented Feb 29, 2024

Describe the bug

Geoalchemy generates the wrong SQL when using a mariadb connection string and the session.add() call, or if I use

session.execute(insert(LocationAlert).values(asdict(map)))

Optional link from https://geoalchemy-2.readthedocs.io which documents the behavior that is expected

No response

To Reproduce

import asyncio
from geoalchemy2 import WKBElement, Geometry
from sqlalchemy import UUID
from sqlalchemy import String
from sqlalchemy.orm import Mapped
from sqlalchemy.orm import mapped_column
from sqlalchemy.orm import MappedAsDataclass
from sqlalchemy.orm import DeclarativeBase
from sqlalchemy.ext.asyncio import async_sessionmaker
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy.ext.asyncio import AsyncAttrs
import uuid

#engine = create_async_engine("mariadb+asyncmy://user:pass@mariadb/gis", echo=True)
engine = create_async_engine("mariadb+asyncmy://root:pencil1@mariadb/orthoplex", echo=True)
async_session = async_sessionmaker(engine, expire_on_commit=False)

class Base(AsyncAttrs, DeclarativeBase):
    pass

class LocationAlert(MappedAsDataclass, Base, repr=False, unsafe_hash=True):
    __tablename__ = "location_alerts"
    psap: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
    mapped_geom: Mapped[WKBElement] = mapped_column(Geometry, index=True)
    id: Mapped[UUID] = mapped_column(UUID, primary_key=True, default_factory=uuid.uuid4)

async def async_main():
    async with engine.begin() as conn:
        await conn.run_sync(Base.metadata.drop_all)
        await conn.run_sync(Base.metadata.create_all)

    async with async_session() as session:
        map = LocationAlert(psap='psap', mapped_geom='POINT(0 0)')
        session.add(map)
        await session.commit()

if __name__ == "__main__":
    asyncio.run(async_main())

Error

[SQL: INSERT INTO location_alerts (psap, call_type, dst_agency, units, mapped_geom, show_supps, id) VALUES (%s, %s, %s, %s, ST_GeomFromEWKT(%s), %s, %s)]
[parameters: ('psap', 'test', 'test', '"[\\"test\\"]"', 'POINT(0 0)', 0, '79a43f96f5164d92a0ced19fa941cc63')]

Additional context

If I use a mysql connection string, or change it to

GeoAlchemy 2 Version in Use

master

Python Version

3.11

Operating system

Linux

@tebrown tebrown added the bug label Feb 29, 2024
@tebrown
Copy link
Contributor Author

tebrown commented Feb 29, 2024

Please assign to me

@adrien-berchet
Copy link
Member

The sentence in Additional context is not complete, did you mean that it works for MySQL but not for MariaDB?

@adrien-berchet
Copy link
Member

And to make it easier to debug I suggest to remove all async stuff if it's possible.

@tebrown
Copy link
Contributor Author

tebrown commented Feb 29, 2024

Yup and yup! It does work with the mysql connection string. I don't know why it worked yesterday, but I'll figure it out!

@adrien-berchet
Copy link
Member

adrien-berchet commented Feb 29, 2024

I think I found the issue 😄
We forgot to change the function names in the compiles decorators here: https://github.com/geoalchemy/geoalchemy2/blob/master/geoalchemy2/admin/dialects/mysql.py#L179

@tebrown
Copy link
Contributor Author

tebrown commented Feb 29, 2024

I think I found the issue 😄 We forgot to change the function names in the compiles decorators here: https://github.com/geoalchemy/geoalchemy2/blob/master/geoalchemy2/admin/dialects/mysql.py#L179

Did I mention I am worst??

@adrien-berchet
Copy link
Member

Ahah 😂
I missed it too so we are equal 😜

@tebrown
Copy link
Contributor Author

tebrown commented Feb 29, 2024

Whatya know? It fixed it! Thanks @adrien-berchet!

@adrien-berchet
Copy link
Member

https://pypi.org/project/GeoAlchemy2/0.14.6/ is up with MariaDB support

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

Successfully merging a pull request may close this issue.

2 participants