I have a custom array type in Postgres:
CREATE TYPE core.arr_message_input AS (
idmessage uuid,
idplugin integer,
versionplugin numeric,
ttl integer
);
There is a simple function to add records to the table:
CREATE OR REPLACE FUNCTION queue_push(
arr_message_input[])
RETURNS Bool
LANGUAGE 'plpgsql'
AS $$
BEGIN
INSERT INTO queue(idmessage, idplugin, versionplugin, queuetime, ttl)
SELECT idmessage, idplugin, versionplugin, now(), ttl
FROM unnest ($1);
RETURN True;
EXCEPTION
WHEN others THEN
RETURN False;
END $$;
Filling in values from Postgres is easy:
SELECT queue_push(
array[
('e62c7924-2cd1-4dd6-9b55-d4e612816ce0', 2, 0, 0),
('a7e864af-4c4c-452d-9df2-f9d4f70ac02e', 2, 0, 0),
]::arr_message_input[]
);
But through SQLAlchemy I can't figure out how to do it. I pass it a list as an array, but there should be a list of lists or something similar. And I have no way of doing it from Python.
For example:
The function model is described as follows:
class QueuePush(GenericFunction):
name = "queue_push"
@staticmethod
def mapped_objects(**kwargs):
return select(
[
Column('queue_push', BOOLEAN),
]
).select_from(
func.queue_push(
kwargs['arr_message_input'],
)
).alias(name="queue_push")
Request to the function:
import QueuePush
messages = [
['027d6e96-84b7-4f10-8640-13dfa1b05fd8', 3, 0, 2],
]
queue = db.query(QueuePush.mapped_objects(arr_message_input=messages)).all()
But still, the created type is a kind of data structure. I'm obviously doing something wrong.