rlfprog

Conversation Schemas & Data

Jan 5th, 2015
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 3.92 KB | None | 0 0
  1. DATABASE runs ON PostgreSQL 9.3.4 ON Debian "wheezy"
  2.  
  3. TABLE "public.auth_user"
  4.     COLUMN    |           TYPE           |                       Modifiers                        
  5. --------------+--------------------------+--------------------------------------------------------
  6.  id           | INTEGER                  | NOT NULL DEFAULT NEXTVAL('auth_user_id_seq'::regclass)
  7.  password     | CHARACTER VARYING(128)   | NOT NULL
  8.  username     | CHARACTER VARYING(30)    | NOT NULL
  9. Indexes:
  10.     "auth_user_pkey" PRIMARY KEY, btree (id)
  11.     "auth_user_username_key" UNIQUE CONSTRAINT, btree (username)
  12.     "auth_user_username_like" btree (username varchar_pattern_ops)
  13. Referenced BY:
  14.     TABLE "conversation_user" CONSTRAINT "conversation_user_user_id_fkey" FOREIGN KEY (user_id) REFERENCES auth_user(id) DEFERRABLE INITIALLY DEFERRED
  15.     TABLE "message" CONSTRAINT "message_from_user_id_fkey" FOREIGN KEY (from_user_id) REFERENCES auth_user(id) DEFERRABLE INITIALLY DEFERRED
  16.  
  17.  
  18. TABLE "public.conversation"
  19.    COLUMN   |           TYPE           |                         Modifiers                        
  20. ------------+--------------------------+-----------------------------------------------------------
  21.  id         | INTEGER                  | NOT NULL DEFAULT NEXTVAL('conversation_id_seq'::regclass)
  22.  start_date | TIMESTAMP WITH TIME zone | NOT NULL
  23. Indexes:
  24.     "conversation_pkey" PRIMARY KEY, btree (id)
  25. Referenced BY:
  26.     TABLE "conversation_user" CONSTRAINT "conversation_id_refs_id_4344ca71" FOREIGN KEY (conversation_id) REFERENCES conversation(id) DEFERRABLE INITIALLY DEFERRED
  27.     TABLE "message" CONSTRAINT "message_conversation_id_fkey" FOREIGN KEY (conversation_id) REFERENCES conversation(id) DEFERRABLE INITIALLY DEFERRED
  28.  
  29.  
  30. TABLE "public.conversation_user"
  31.      COLUMN      |  TYPE   |                           Modifiers                            
  32. -----------------+---------+----------------------------------------------------------------
  33.  id              | INTEGER | NOT NULL DEFAULT NEXTVAL('conversation_user_id_seq'::regclass)
  34.  conversation_id | INTEGER | NOT NULL
  35.  user_id         | INTEGER | NOT NULL
  36. Indexes:
  37.     "conversation_user_pkey" PRIMARY KEY, btree (id)
  38.     "conversation_user_conversation_id_user_id_key" UNIQUE CONSTRAINT, btree (conversation_id, user_id)
  39.     "conversation_user_conversation_id" btree (conversation_id)
  40.     "conversation_user_user_id" btree (user_id)
  41. Foreign-KEY constraints:
  42.     "conversation_id_refs_id_4344ca71" FOREIGN KEY (conversation_id) REFERENCES conversation(id) DEFERRABLE INITIALLY DEFERRED
  43.     "conversation_user_user_id_fkey" FOREIGN KEY (user_id) REFERENCES auth_user(id) DEFERRABLE INITIALLY DEFERRED
  44.  
  45.  
  46. auth_user TABLE DATA:
  47.  id |   username    
  48. ----+---------------
  49.   1 | user1
  50.   3 | user3
  51.   4 | user4
  52.  32 | me
  53. (4 ROWS)
  54.  
  55.  
  56. conversation TABLE DATA:
  57.  id |          start_date          
  58. ----+-------------------------------
  59.   1 | 2015-01-05 12:14:24.098392-08
  60.   2 | 2015-01-05 12:14:24.124386-08
  61.   3 | 2015-01-05 12:14:24.143839-08
  62. (3 ROWS)
  63.  
  64.  
  65.  id | conversation_id | user_id
  66. ----+-----------------+---------
  67.   1 |               1 |      32
  68.   2 |               1 |       3
  69.   3 |               2 |      32
  70.   4 |               2 |       3
  71.   5 |               2 |       4
  72.   6 |               3 |       3
  73.   7 |               3 |       1
  74. (7 ROWS)
  75.  
  76. -- Your audited query altered to determine if there's a conversation
  77. -- between users 32, 3, and 4
  78. SELECT cu1.id
  79. FROM   conversation_user cu1
  80. WHERE  cu1.user_id = 32
  81. AND    EXISTS (
  82.    SELECT 1
  83.    FROM   conversation_user cu2
  84.    WHERE  cu2.conversation_id = cu1.conversation_id
  85.    AND    cu2.user_id = 3
  86.    )
  87. AND    EXISTS (
  88.    SELECT 1
  89.    FROM   conversation_user cu3
  90.    WHERE  cu3.conversation_id = cu1.conversation_id
  91.    AND    cu3.user_id = 4
  92.    )
  93. AND NOT EXISTS (
  94.    SELECT 1
  95.    FROM   conversation_user cu4
  96.    WHERE  cu4.conversation_id = cu1.conversation_id
  97.    AND    cu4.user_id NOT IN (3,4,32)
  98.    );
Advertisement
Add Comment
Please, Sign In to add comment