Code: Select all
create table
public.prueba (
id smallint generated by default as identity not null,
name text null,
age smallint null,
created_at timestamp without time zone null,
punto geography null,
constraint prueba_pkey primary key (id)
) tablespace pg_default;
Code: Select all
class Prueba(Base):
__tablename__ = "prueba"
id: Mapped[int] = mapped_column(
sa.SmallInteger, sa.Identity(start=1), primary_key=True
)
name: Mapped[str] = mapped_column(sa.String(50), nullable=False)
age: Mapped[int] = mapped_column(sa.Integer, nullable=False)
created_at: Mapped[datetime] = mapped_column(sa.DateTime, default=datetime.now)
punto: Mapped[WKBElement] = mapped_column(
Geometry(geometry_type="POINT", srid=4326, spatial_index=True)
)
Der Code, den ich zum Hinzufügen verwende Die Daten lauten wie folgt:
Code: Select all
prueba = Prueba(
name="Prueba_2",
age=5,
created_at=datetime.now(),
punto="POINT(-1.0 1.0)",
)
with Session() as session:
session.add(prueba)
session.commit()
Code: Select all
File "c:...\.venv\Lib\site-packages\sqlalchemy\engine\default.py", line 941, in do_execute
cursor.execute(statement, parameters)
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedFunction) function st_geomfromewkt(unknown) does not exist
LINE 1: ...a_2', 5, '2024-12-28T18:49:07.130429'::timestamp, ST_GeomFro...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
[SQL: INSERT INTO prueba (name, age, created_at, punto) VALUES (%(name)s, %(age)s, %(created_at)s, ST_GeomFromEWKT(%(punto)s)) RETURNING prueba.id]
[parameters: {'name': 'Prueba_2', 'age': 5, 'created_at': datetime.datetime(2024, 12, 28, 18, 49, 7, 130429), 'punto': 'POINT(-1.0 1.0)'}]
(Background on this error at: https://sqlalche.me/e/20/f405)
Außerdem habe ich versucht, einen ähnlichen Ansatz wie in diesem Tutorial zu verwenden und die Daten mit diesem Code hinzuzufügen:
Code: Select all
punto=WKBElement("POINT(10 25)", srid=4326),
Code: Select all
File "c:\...\back\main.py", line 16, in main
punto=WKBElement("POINT(10 25)", srid=4326),
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\...\.venv\Lib\site-packages\geoalchemy2\elements.py", line 201, in __init__
header = binascii.unhexlify(data[:18])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
binascii.Error: Non-hexadecimal digit found