Storing multiple values in one column - python

I am designing a web application that has users becoming friends with other users. I am storing the users info in a database using sqlite3.
I am brainstorming on how I can keep track on who is friends with whom.
What I am thinking so far is; to make a column in my database called Friendships where I store the various user_ids( integers) from the user's friends.
I would have to store multiple integers in one column...how would I do that?
Is it possible to store a python list in a column?
I am also open to other ideas on how to store the friendship network information in my database....
The application runs through FLASK

What you are trying to do here is called a "many-to-many" relationship. Rather than making a "Friendships" column, you can make a "Friendship" table with two columns: user1 and user2. Entries in this table indicate that user1 has friended user2.

It is possible to store a list as a string into an sql column.
However, you should instead be looking at creating a Friendships table with primary keys being the user and the friend.
So that you can call the friendships table to pull up the list of friends.
Otherwise, I would suggest looking into a Graph Database, which handles this kind of things well too.

If you want to organize correct storage of data you should know more about relative databases. I recommend you to read this first of all. With some normalization it would perform better (some operations on db will be much more simplier).
As mentioned before you should make another table with friendships to perform first normal form. It would be much easier for you to perform modification of relationships.

Related

Sorting and Filtering multiple queries of the same collection in Firestore

I'm new on cloud firestore and I'm trying to make queries as efficient as possible but I kind of desperate with an specific one. I would greatly appreciate your help.
This is the situation:
I want to show a project list which that I'm getting from an user field and 2 queries in project entity. The user field let’s called "favorite projects" and it has the projects id that reference those projects on their entity. The other query retrieve me the public projects (==) and the last the private projects where the user is a contributor (array_contains).
I want to sort and filtering the result of the two queries. Is there an option to merge both queries and use sort and filter as a we do with a collection reference?
Thank you for your time, have a nice day!
Based on this and this documentation, I do not believe there is an out of the box solution for joining the results of queries such as the ones described.
You'll need to achieve that within the your code.
For example you can run the first query and store all the data of the document in a map or array. Then use the reference of the other document within the document_reference to make the second query and the third.
Once you have all of them you can do as you please using Python. But getting them ready using a single query or auto-joining the queries seems to not be supported yet.

Is any variable in PostgreSQL to store a list

We have a ticket software to manage our work, every ticket is assigned to a tech in one field -the normal stuff-, but now we want to assign the same ticket to several technicians eg: tick 5432: tech_id(2,4,7) where 2,4,7 are tech IDs.
Of course we can do that using a separate table with the IDs of the tech and the ticket ID, but we have to convert the data.
The "right" way to do this is to have a separate table of ticket
assignments. Converting the data for something like this is fairly simple on the database end. create table assign as select tech_id from ... followed by creating any necessary foreign key constraints.
Rewriting your interface code can be trickier, but you're going to
have to do that anyway to allow for more than one tech.
You could use an array type, but sometimes database
interfaces don't understand postgres array types. There isn't anything
inherent in arrays that prevents duplicates or imposes ordering, but
you could do that with an appropriate trigger.

Python: Dumping Database Data with Peewee

Background
I am looking for a way to dump the results of MySQL queries made with Python & Peewee to an excel file, including database column headers. I'd like the exported content to be laid out in a near-identical order to the columns in the database. Furthermore, I'd like a way for this to work across multiple similar databases that may have slightly differing fields. To clarify, one database may have a user table containing "User, PasswordHash, DOB, [...]", while another has "User, PasswordHash, Name, DOB, [...]".
The Problem
My primary problem is getting the column headers out in an ordered fashion. All attempts thus far have resulted in unordered results, and all of which are less then elegant.
Second, my methodology thus far has resulted in code which I'd (personally) hate to maintain, which I know is a bad sign.
Work so far
At present, I have used Peewee's pwiz.py script to generate the models for each of the preexisting database tables in the target databases, then went and entered all primary and foreign keys. The relations are setup, and some brief tests showed they're associating properly.
Code: I've managed to get the column headers out using something similar to:
for i, column in enumerate(User._meta.get_field_names()):
ws.cell(row=0,column=i).value = column
As mentioned, this is unordered. Also, doing it this way forces me to do something along the lines of
getattr(some_object, title)
to dynamically populate the fields accordingly.
Thoughts and Possible Solutions
Manually write out the order that I want stuff in an array, and use that for looping through and populating data. The pros of this is very strict/granular control. The cons are that I'd need to specify this for every database.
Create (whether manually or via a method) a hash of fields with an associated weighted value for all possibly encountered fields, then write a method for sorting "_meta.get_field_names()" according to weight. The cons of this is that the columns may not be 100% in the right order, such as Name coming before DOB in one DB, while after it in another.
Feel free to tell me I'm doing it all wrong or suggest completely different ways of doing this, I'm all ears. I'm very much new to Python and Peewee (ORMs in general, actually). I could switch back to Perl and do the database querying via DBI with little to no hassle. However, it's libraries for excel would cause me as many problems, and I'd like to take this as a time to expand my knowledge.
There is a method on the model meta you can use:
for field in User._meta.get_sorted_fields():
print field.name
This will print the field names in the order they are declared on the model.

How to join two data models for google app engine in python

I am trying/lerning to make application in python that will have information about universitys and their departments.
The problem I have is that I want to use data models, (I know very little about them).
I want to have two databases. One will contain departments, while other will contain universitys and list of departments.
Thanks for help!!!
Thanks for fast answers. I found some tutorials like:
http://www.youtube.com/watch?v=rRCx9e38yr8&list=PLDA31F43DE4107B05
http://blog.notdot.net/2010/10/Modeling-relationships-in-App-Engine
and I started reading them, and some thing are having more sense.
In case you are referring to the structure of your database you might want to read more about E-R Diagrams and Relational Database. I can brief you with the relational database model which might be useful in your case.
At minimum I think it can have three tables in the same database. One for the Department ids mapping to the universities. Another one mapping the departments with course names and unique ids to the departments which offer that course. And the third one mapping the unique student ids with the course ids. Then you can have additional tables mapping from student information to student id. And professor ids with professor info, professor ids with course ids which they would be teaching....and this can go on based on the data you want to store.
Also keep in mind in case you want to create foreign keys for the ids you should use InnoDB Engine as MyISAM doesnot allow foreign keys.

Is there a way to set all db queries to only retrieve a given user's entries, or would each query throughout the app have to include "AND user..."?

I created an app that lets users input products and compare them. I am at the point where i need to consider scaling and multiple users, and I want to only show users the items that they've created (instead of the entire db, which may be an option at some point, but for now each user has to create their own lists).
Is there a canned/preferred way of making all queries throughout an app only return entries with the "author" field set to a certain user, the logged in user? Or does every query have to be updated with "AND author..." type filter? Is this what namespaces are for?
I'm using Python on Google App Engine.
I would suggest looking into namespaces, it is exactly the type of use-case they are well suited for.
One note, if you want to be able to query users all users' products, you may be better off adding an owner property and using query filters. You can't currently query across namespaces -- only within a single namespace at one time.

Categories

Resources