sqlAlchemy schema - Multiple optional columns - sql

I have an app built on Flask, using sqlalchemy as an ORM, it will have books with an upvote system based on multiple languages as columns, so if a book has been translated to 20 languages, each language will have its own voting score.
Some books could have 30 languages available and some only one.
Is it possible to have multiple columns being optional?
Any pointers to how I should tackle this?
I am fairly new to sql. So I'm not sure how to do proper research on this. I was following Miguel Grinberg's Micro blog tutorial and while his followers system is somewhat similar, I'm not sure how to extend it to my case. Any help is appreciated.

There's many possible ways to do this.
I would probably do something like:
Have model/table books.
Have model/table languages.
Have model/table book_in_language, which has some rating (which is probably aggregated from user_book_in_language_rating table.

Related

Database relationship alias naming phrase

I am aware of database relations, I was taught similar like this below:
I am developing an web apps with strapi cms, in there I can see some relations those are not named academic like one to one, one to many etc. vice-versa. Instead they used has one, belongs to many something like this. Not only strappy, I have also encountered this in some other ORM documentations as well. Which is completely okay, but I am not understanding to relate with the above image.
In stripy also the relational symbols are different than the above image.
For example:
Has One:
Has and belongs to one
Belongs to many
Has Many
Has and belongs to many
Has many
I need to know these phrases to relate, is one to many similar to has many?, is one to one similar to has one? etc. Also, what is the benefit of use these phrases rather than one to one etc.? Is it a topic in DB systems I may be missed?
There are lots of different notations for describing relationships, each with its own vocabulary. There’s a reasonable description of them here.
On top of these, individual products may use their own, non-standard, notations

Migration guide to move from SQL to CouchDB

My question: Would you have some web pages (or tutorial) to point me to, that would focus on migrating guide from SQL to NoSQL, especially for 1-N relationship modelling?
A bit more on my needs:
I have to develop a CouchDB-based application in which I have several 1-N relationships between about 10 different types of objects.
The application needs to do many different queries to filter and sort the data based on fields that belong to the several types of objects. It would be so easy to do in SQL, but with CouchDB I fail.
I know, things have to be thought differently between SQL and NoSQL, but despite all the documents/forums/etc. I could read, I could find no guidelines to change my way of thinking and to be a good NoSQL man, and write my queries.
I have also tried the Mango queries which were looking somehow more powerful than map/reduce in some cases, so this in option of interest for me too.
Many thanks!
btw there is also this page in the Couchdb doc itself, which brings some basic answers: http://docs.couchdb.org/en/2.1.1/ddocs/views/nosql.html

SQL - Alternatives to a generic datamodel

while studying, im struggling to find an answer to this question:
What is a generic datamodel?
Are there any alternatives to a generic datamodel, if so; which one would you recommend?
I've looked everywhere to find a decent answer, but i didn't find it. It might be just a very simple question, i just couldn't find these terms; 'generic datamodel'.
Perhaps by generic data model you mean an entity attribute value model: EAV on wikipedia
This data model has one table and three columns. It is usually burnt by fire if ever discovered by someone of a database (vs developer) persuasion.
The thing with a data model is that you are modeling something. You are applying your skills to build a database that is optimised to store and retrieve data about something (widgets in a factory, trades on a stock exchange, Facebook posts). A EAV can store all of this stuff but it causes many other problems down the line.
More info here: EAV
It could be possible its an EAV mode. But that leaves me with the following question: are there any alternatives to this model?
Thanks.
# nick, this is the question:
Advise in Chapter 5 of the people involved in your case about the application or not of a generic data model
and optionally other alternatives. Motivate your opinion (10 points).

Are there any open source resources for SQL schema design patterns?

I can barely count the number of times I've created a "users" table, similar for "computers" and "customers". I've tried looking around, but haven't ever seen a resource for modeling these schema that we see over and over again. It seems like some of these objects should be some-kind-of-solved by now. Is there anything like this?
I have never seen anything like this either and I'm not sure it's necessary. Yes, there are a lot of similarities but every application is different. At one point I had built an internal library of some of my more "standard" tables (user is a good example) to use as a jumping point, but I have yet to create two identical tables for different systems.
Thus, I have yet to ever use the library I built because I can write the new table quicker and more error free than I can modify another existing example to work for the current project.
You could look at the source code of some popular open-source CRM/ERPs, such as OpenERP, though some of them are not great.
These are the top books on data modelling patterns:
Analysis Patterns, Fowler
Data Model Resource Book, vol. 1,2,3, Silverston
Enterprise Model Patterns, Hay
Patterns of Data Modeling, Blaha

How do I structure my database so that two tables that constitute the same "element" link to another?

I read up on database structuring and normalization and decided to remodel the database behind my learning thingie to reduce redundancy.
I have different types of entries that can be learned. Gap texts/cloze tests (one text, many gaps) and simple known-unknown (one question, one answer) types.
Now I'm in a bit of a pickle:
gaps need exactly the same columns in the user table as question-answer types
but they need less columns than question-answer types (all that info is in the clozetests table)
I'm wishing for a "magic" foreign key that can point both to the gap and the terms table. Of course their ids would overlap though. I don't like having both a term_id and gap_id in the user_terms, that seems unelegant (but is the most elegant I can come up with after googling for a while, not knowing what name this pickle goes by).
I don't want a user_gaps analogue to user_terms, because then I'd be in the same pickle when it comes to the table user_terms_answers.
I put up this cardboard cutout collage of my schema. I didn't remove the stuff that isn't relevant for this question, but I can do that if anyone's confusion can be remedied like that. I think it looks super tidy already. Tidier than my mental concept of this at least.
Did I say any help would be greatly appreciated? Answerers might find themselves adulated for their wisdom.
Background story if you care, it's not really relevant to the question.
Before remodeling I had them all in one table (because I added the gap texts in a hurry), so that the gap texts were "normal" items without answers, while the gaps where items without questions. The application linked them together.
Edit
I added an answer after SO coughed up some helpful posts. I'm not yet 100% satisfied. I try to write views for common queries to this set up now and again I feel like I'll have to pull application logic for something that is database turf.
As mentioned in the comment, it is hard to answer without knowing the whole story. So, here is a story and a model to match. See if you can adapt this to you example.
School of (foreign) languages offers exams for several levels of language proficiency. The school maintains many pre-made tests for each level of each language (LangLevelTestNo).
Each test contains several (many) questions. Each question can be simple or of the close-text-type. Correct answers are stored for each simple question. Correct terms are stored for each gap of each close-text question.
Student can take an exam for a language level and is presented with one of the pre-made tests. For each student exam, the exam form is maintained which stores students answers for each question of the exam. Like a question, an answer may be of a simple of of a close-text-type.
After editing my question some Stackoverflow started relating the right questions to me.
I knew this was a common problem, but I really couldn't find it, just couldn't come up with the right search terms, I guess.
The following threads address similar problems and I'll try to apply that logic to my own design. They all propose adding a higher-level description for (in my case terms and gaps) like items. That makes sense and reflects the logic behind my application.
Relation Database Design
Foreign Key on multiple columns in one of several tables
Foreign Key refering to primary key across multiple tables
And this good person illustrates how to retrieve the data once it's broken up across tables. He also clues me to the keyword class table inheritance, so now I know what to google.
I'll post back with my edited schema once I've applied this. It does seem more elegant like this.
Edited schema