Tastypie URL Mapping, Mapping with mysql database - tastypie

I have one table:
Table Name: "blog"
version_id(Primary Key) | blog_id | version_name | title | status
1 | 21 | draft | test | 1
2 | 21 | live | test | N/A
Users will use this url:-
GET: v1/blogs/{blog_id}/draft
PUT: v1/blogs/{blog_id}/draft
but tastypie will create url according to primary key: -
GET: v1/blogs/{version_id}
PUT: v1/blogs/{version_id}
According to me Pseudo code should be:-
select version_id
from blog
where version_id = 1 and version_name = draft
and using version_id I can create:-
v1/blogs/{version_id}
But I am not able to write url mapping in tastypie,
Please help

I would setup the blog table as follows
id | blog_id | version | version_name | title | status
1 | 21 | 1 | draft | test | 1
2 | 21 | 2 | live | test | N/A
Its good practice to always have a unique identifier for a table. I'm not quite sure if thats what you meant by version_id since your naming is confusing
tastypie will use the id field, but with that information you can easily find the parent/published blog and change it (since I guess that's what you're after). I'm also not sure what blog_id refers to. Conceptially it refers to the record (ie the id field), but perhaps your setup is different. I suggest you relook at your modelling to make sure its as simple as possible.

Related

PostgreSQL arabic case insensitive

I am looking for how to search a database using Arabic text. In Arabic there are some letters that can be written in different ways but in the results they should all show up if one of them is included in the where clause.
The famous example for this would be:
SELECT * FROM persons WHERE name = "اسامة";
+----+--------------+
| id | name |
+----+--------------+
| 3 | أسامه |
| 4 | أسامة |
| 5 | اسامه |
| 6 | اسَامه |
+----+--------------+
4 rows in set (0.00 sec)
I found a good and probably most performant way to do this by creating a custom collation on MySQL in this article but I have no idea how that is done or if it is possible at all in PostgreSQL.
Other ways that include changing the query itself to use Regex are not useful for my use case.
Can someone please guide me how to do the same

Know which table are affected by a connection

I want to know if there is a way to retrieve which table are affected by request made from a connection in PostgreSQL 9.5 or higher.
The purpose is to have the information in such a way that will allow me to know which table where affected, in which order and in what way.
More precisely, something like this will suffice me :
id | datetime | id_conn | id_query | table | action
---+----------+---------+----------+---------+-------
1 | ... | 2256 | 125 | user | select
2 | ... | 2256 | 125 | order | select
3 | ... | 2256 | 125 | product | select
(this will be the result of a select query from user join order join product).
I know I can retrieve id_conn througth "pg_stat_activity", and I can see if there is a running query, but I can't find an "history" of the query.
The final purpose is to debug the database when incoherent data are inserted into the table (due to a lack of constraint). Knowing which connection do the insert will lead me to find the faulty script (as I have already the script name and the id connection linked).

Getting duplicate rows when querying Cloud SQL in AppMaker

I migrated from Drive tables to a 2nd gen MySQL Google Cloud SQL data model. I was able to insert 19 rows into the following Question table in AppMaker:
+-------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+-------+
| SurveyType | varchar(64) | NO | PRI | NULL | |
| QuestionNumber | int(11) | NO | PRI | NULL | |
| QuestionType | varchar(64) | NO | | NULL | |
| Question | varchar(512) | NO | | NULL | |
| SecondaryQuestion | varchar(512) | YES | | NULL | |
+-------------------+--------------+------+-----+---------+-------+
I queried the data from the command line and know it is good. However, when I query the data in AppMaker like this:
var newQuery = app.models.Question.newQuery();
newQuery.filters.SurveyType._equals = surveyType;
newQuery.sorting.QuestionNumber._ascending();
var allRecs = newQuery.run();
I get 19 rows with the same data (the first row) instead of the 19 different rows. Any idea what is wrong? Additionally (and possibly related) my list rows in AppMaker are not showing any data. I did notice that _key is not being set correctly in the records.
(Edit: I thought maybe having two columns as the primary key was the problem, but I tried having the PK be a single identity column, same result.)
Thanks for any tips or pointers.
You have two primary key fields in your table, which is problematic according to the App Maker Cloud SQL documentation: https://developers.google.com/appmaker/models/cloudsql
App Maker can only write to tables that have a single primary key
field—If you have an existing Google Cloud SQL table with zero or
multiple primary keys, you can still query it in App Maker, but you
can't write to it.
This may account for the inability of the view to be able to properly display each row and to properly set the _key.
I was able to get this to work by creating the table inside AppMaker rather than using a table created directly in the Cloud Shell. Not sure if existing tables are not supported or if there is a bug in AppMaker, but since it is working I am closing this.

Is it possible to get 3 select query results by executing only one stored procedure?

I have to display data in the following format
-----------------------------------------------------------
| Group Name | Description | Assigned Users | Super Groups|
-----------------------------------------------------------
|Group1 | Blah Blah | User1 | SPG1 |
| | | User2 | SPG3 |
| | | User3 | |
-----------------------------------------------------------
| Group2 | More Blah | User1 | SPG5 |
| | | User13 | |
-----------------------------------------------------------
Assigned users and Super groups data are coming from unrelated tables. Now I wonder whether is it possible to get 3 select query results in one shot (i.e. the same procedures returns 3 results). Otherwise I'm going to query the groups and users first, get the group IDs then query super groups.
So again, Is it possible to get 3 select query results by executing only one stored procedure?
Yes, just include 3 select statements.
If you're consuming these in .net and storing them in a DataSet you'll have 3 tables in the DataSet.
Example:
create procedure test
as
select 1 as res1;
select 2 as res2;
select 3 as res3
exec test
Yes. You'll have to include the three statements in your stored procedure. Take a look at this post.

Database Design - Items and Regions

I have design problem of database structure.
The goal is to have database for offers that our clients offer per some geographical region.
Each offer can be offered in many regions.
The regions are in hierarhy - example:
subregion_1
subregion_11
region_111
region_112
subregion_12
region_121
region_122
subregion_2
subregion_21
region_221
Now I want to store in database the offer_1 and regions for that offer. I will give You 3 examples what I have to ahieve:
when my offer_1 is stored in region_111 then I would like to display this offer when user are browsing the subregion_1, subregion_11 and region_111
If offer_1 is stored in regions subregion_11 and region_121 then the offer should be displayed when user are browsing the subregion_1, subregion_11 and all branch of subregion_11, subregion_12 and region_121
when my offer_1 is stored in subregion_1 then the offer is displayed on subregion_1 page and all branch under subregion_1.
Also I have to provide a way to calculate the number of diffrent offers in each regions dynamicaly and very fast.
Does somebody have some advice how to aproach this design?
Here is what I have so Far.
Regions
------------------------------------------------------------
| id | level1 | level2 | level3 | name | level |
------------------------------------------------------------
| 02 | 02 | null | null | subregion_1 | 1 |
| 0201 | 02 | 01 | null | subregion_11 | 2 |
| 020103 | 02 | 01 | 03 | region_111 | 3 |
------------------------------------------------------------
Offers to regions
------------------------
| offer_id | region_id |
------------------------
| 1 | 020103 |
| 1 | 0202 |
------------------------
I created id for regions from concatenating level1, level2 and level3. In the table Offers_to_regions I store the offer and the region. Here I have region on level 3 (020103) and region on level 2 (0202) for offer 1.
With this design I have problems how to query the numbers of difrent offers per region, and how to query offers for regions on level1, level2 and level3 regions.
Well there is the obvious way which uses an id to point to a parent like this
CREATE TABLE Regions (
region_id INT AUTO_INCREMENT PRIMARY KEY,
parent_id INT,
region_name VARCHAR(100) NOT NULL,
FOREIGN KEY (parent_id) REFERENCES Regions(region_id)
);
But in your situation this could be considered an anti-pattern, since it is not so easy to query through the hierarchy (specially if the number of levels changes)
Another approach could be using something like Path Enumeration, where you store the hierarchy path similar to for example unix paths. E.g.
CREATE TABLE Regions (
region_id INT AUTO_INCREMENT PRIMARY KEY,
path VARCHAR(100),
region_name VARCHAR(100) NOT NULL
);
This will allow you to store your hierarchy like this
---------------------------------------------
| region_id | path | region_name |
---------------------------------------------
| 1 | 1/ | subregion_1 |
| 2 | 1/2/ | subregion_11 |
| 3 | 1/2/3/ | region_111 |
| 4 | 1/2/4/ | region_112 |
---------------------------------------------
This way, when querying your offers table (where each offer will have a ref. to the region_id), and while browsing lets say offer for the subregion_1 (with id 1) your query can look something like this.
select Offers.SOME_COLUMN, ......
from Offers, Regions
where Offers.region_id = Regions.region_id
and Regions.path like '1/%'
There are other patterns to model your hierarchical data, such as Nested Sets and Closure Table (maybe relevant) which you might be interested to look into as well. each has different pros and cons in terms of select/insert/delete performance
EDIT:
I just noticed you edited your question, also that offers could belong to more than one region. The above might need adjustments to support assigning more than one region, but the basic idea could still be applied.