如何在PostgreSQL中使用row_to_json分配JSON变量

时间:2018-10-05 23:53:29

标签: json postgresql postgresql-9.5

我正在尝试在PostgreSQL中创建一个匿名函数来为应用程序创建模拟数据。.我想先执行SELECT查询(从随机宪章中获取数据),然后将所有行转换为JSON row_to_json,然后将结果分配给JSON类型的变量。

我需要此租船信息,以便可以将其添加到预订表中。

这不起作用,我想我不知道如何将select的结果与先前创建的变量相关联;我收到charterData为null的错误,我想知道如何实现这一点。

这是SQL中的匿名函数:

BEGIN;
    DO $$
    DECLARE charterData JSON;
    DECLARE bookingId INTEGER;
    BEGIN
        SELECT row_to_json(t) INTO charterData FROM (select charter_id, name from charters) t WHERE charter_id = 1;
        INSERT INTO bookings (charter, yacht, email, date, guests, total, start_hour, end_hour, hotel, arrival_date) values (charterData, '{"test":1}', 'a', '12/10/1995', 8, '78', '123', '123', '123', '123')
        RETURNING booking_id INTO bookingId;
    END $$;
COMMIT;

表租约:

                                        Table "public.charters"
   Column    |       Type        | Collation | Nullable |                   Default                    
-------------+-------------------+-----------+----------+----------------------------------------------
 charter_id  | integer           |           | not null | nextval('charters_charter_id_seq'::regclass)
 name        | character varying |           | not null | 
 description | character varying |           | not null | 
 sail_hours  | integer           |           | not null | 
Indexes:
    "charters_pk" PRIMARY KEY, btree (charter_id)
    "name_charter" UNIQUE CONSTRAINT, btree (name)
Referenced by:
    TABLE "bookings" CONSTRAINT "charters_bookings_fk" FOREIGN KEY (charter) REFERENCES charters(name) ON DELETE CASCADE
    TABLE "pricing" CONSTRAINT "charters_pricing_fk" FOREIGN KEY (charter_id) REFERENCES charters(charter_id) ON DELETE CASCADE

预订表:

                                         Table "public.bookings"
     Column     |       Type        | Collation | Nullable |                   Default                    
----------------+-------------------+-----------+----------+----------------------------------------------
 booking_id     | integer           |           | not null | nextval('bookings_booking_id_seq'::regclass)
 charter        | json              |           | not null | 
 yacht          | json              |           | not null | 
 email          | character varying |           | not null | 
 date           | date              |           | not null | 
 guests         | integer           |           | not null | 
 total          | numeric           |           | not null | 
 start_hour     | character varying |           | not null | 
 end_hour       | character varying |           | not null | 
 alcohol        | character varying |           |          | 
 transportation | character varying |           |          | 
 others         | character varying |           |          | 
 arrival_date   | character varying |           |          | 
 hotel          | character varying |           |          | 
Indexes:
    "bookings_pk" PRIMARY KEY, btree (booking_id)
    "end_hour" UNIQUE CONSTRAINT, btree (end_hour)
    "start_hour" UNIQUE CONSTRAINT, btree (start_hour)
Foreign-key constraints:
    "charters_bookings_fk" FOREIGN KEY (charter) REFERENCES charters(name) ON DELETE CASCADE
    "yachts_bookings_fk" FOREIGN KEY (yacht) REFERENCES yachts(name) ON DELETE CASCADE
Referenced by:
    TABLE "bookings_extra" CONSTRAINT "bookings_extra_fk" FOREIGN KEY (booking_id) REFERENCES bookings(booking_id) ON DELETE CASCADE

1 个答案:

答案 0 :(得分:0)

好的,我已经找到答案了……有点傻,但是也许这个答案会帮助到某人

BEGIN;
    DO $$
    DECLARE charter JSON;
    DECLARE bookingId INTEGER;
    BEGIN
        charter := (SELECT row_to_json(t) FROM (SELECT charter_id, name FROM charters) t WHERE charter_id = $1);
        INSERT INTO bookings
            (charter, yacht, email, date, passengers, total, start_hour, end_hour, hotel, arrival_date, charter_price)
        values (charter, '{"test":1}', 'a', '12/10/1995', 8, '78', '123', '123', '123', '123', '132')
            RETURNING booking_id INTO bookingId;
    END $$;
COMMIT;