postgresql-9.3

Check if a Postgres JSON array contains a string

♀尐吖头ヾ 提交于 2019-12-17 22:04:47
问题 I have a table to store information about my rabbits. It looks like this: create table rabbits (rabbit_id bigserial primary key, info json not null); insert into rabbits (info) values ('{"name":"Henry", "food":["lettuce","carrots"]}'), ('{"name":"Herald","food":["carrots","zucchini"]}'), ('{"name":"Helen", "food":["lettuce","cheese"]}'); How should I find the rabbits who like carrots? I came up with this: select info->>'name' from rabbits where exists ( select 1 from json_array_elements(info-

sqlalchemy dynamic schema on entity at runtime

喜欢而已 提交于 2019-12-17 18:29:43
问题 I'm using SQL Alchemy and have some schema's that are account specific. The name of the schema is derived using the account ID, so I don't have the name of the schema until I hit my application service or repository layer. I'm wondering if it's possible to run a query against an entity that has it's schema dynamically set at runtime? I know I need to set the __table_args__['schema'] and have tried doing that using the type() built-in, but I always get the following error: could not assemble

How to create index on json field in Postgres 9.3

廉价感情. 提交于 2019-12-17 08:10:11
问题 In PostgreSQL 9.3 Beta 2 (?), how do I create an index on a JSON field? I tried it using the -> operator used for hstore but got the following error: CREATE TABLE publishers(id INT, info JSON); CREATE INDEX ON publishers((info->'name')); ERROR: data type json has no default operator class for access method "btree" HINT: You must specify an operator class for the index or define a default operator class for the data type. 回答1: Found: CREATE TABLE publishers(id INT, info JSON); CREATE INDEX ON

Get sum of integers for UNIQUE ids

自作多情 提交于 2019-12-14 03:12:49
问题 In a game using PostgreSQL 9.3 as backend I am trying to limit the number of games played by a user per week. I have prepared an SQL Fiddle, but unfortunately it doesn't work. My (test, not production) code is here: create table pref_users ( id varchar(32) primary key, last_ip inet ); create table pref_match ( id varchar(32) references pref_users on delete cascade, completed integer default 0 check (completed >= 0), yw char(7) default to_char(current_timestamp, 'IYYY-IW'), primary key(id, yw)

PostgreSQL 9.3: Function is not unique error

 ̄綄美尐妖づ 提交于 2019-12-13 13:13:34
问题 I have created the following function with 11 parameters as shown in below: Function calling : SELECT * FROM function_TableDetails ( NULL::Type_Table, '2671ffdb-28a4-4ce4-a226-e5a21d66509e', 'D09636DC-0185-4FFC-AEDD-63895B445CD8', 'Xyz', 'Cola', '20028243-52c2-4d23-a6fe-c3694aab84a2', '01-01-2000', '01-01-2016', '00:00:01', '23:59:59', 'Al' ); Butting the following error: ERROR: function function_TableDetails(Type_Table, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown,

JPA query using a database function to use a specific index

自闭症网瘾萝莉.ら 提交于 2019-12-13 06:33:42
问题 I have a table named vehicle which has a column named VIN. Users need the ability to search based on the last 6 characters of the VIN. So I've added an index in Postgres like so: CREATE INDEX "ix_vin_last_6" ON vehicle(RIGHT(vin,6)); I added a few test records as follows: LAST6VIN_A_123456 LAST6VIN_B_123456 LAST6VIN_C_123456 Using SpringDataJPA repository, here is one way to query for the above test records: List<Vehicle> findByVinEndingWith(String vin) But the JPA query generated above uses

Update with results of another sql

邮差的信 提交于 2019-12-13 05:26:48
问题 With the sql below I count how many records I have in tableB for each code. The total field is assigned the result of the count and the code the code field of the record. SELECT "count" (*) as total, tableB."code" as code FROM tableB WHERE tableB.code LIKE '%1' GROUP BY tableB.code In tableA I have a sequence field and I update with the result of total (obtained in the previous sql) plus 1 Do this for each code. I tried this and it did not work, can someone help me? UPDATE tableA SET tableA

How to add a key to a JSON array value?

别来无恙 提交于 2019-12-13 02:42:38
问题 I am new to Postgres and using version 9.4. I have a query returning a json column. How can I add a key to a JSON array value ? My query: select array_to_json(array_agg(t)) from (select DISTINCT ON(city,state)latitudes,longitudes,city,state from zips where city ilike 'ORL%' order by city,state,ziptype desc limit 10) t; The output is like: [{"latitudes": 31.22,"longitudes": -103.91,"city": "Orla","state": "TX"}, ... However, I would like to name it such as: ["Locations": [{"latitudes": 31.22,

PostgreSQL 9.3: Filter in Pivot table query

吃可爱长大的小学妹 提交于 2019-12-13 02:27:56
问题 Well I have this records: Employee_Number Employee_role Group_Name ---------------------------------------------------- EMP101 C# Developer Group_1 EMP102 ASP Developer Group_1 EMP103 SQL Developer Group_2 EMP104 PLSQL Developer Group_2 EMP101 Java Developer EMP102 Web Developer EMP101 DBA EMP105 DBA EMP106 SQL Developer Group_3 EMP107 Oracle Developer Group_3 EMP101 Oracle Developer Group_3 Want to show the pivot table for above records in the following format: Employee_Number TotalRoles

Will changing column from timestamp to timestamptz lock the table?

半腔热情 提交于 2019-12-12 20:06:30
问题 I want to migrate a column from timestamp (no timezone) to timestamptz type. I'm on Postgres 9.3.9. I need to know if this operation will cause a table rewrite (lock the table) as my table is large and the db is live. I found this in the 9.2 release notes: Increasing the length limit for a varchar or varbit column, or removing the limit altogether, no longer requires a table rewrite. Similarly, increasing the allowable precision of a numeric column, or changing a column from constrained