Mismatch between MySQL and Python - python

I know the mismatch between Object Oriented Technology and the Relational Technology, generally here.
But I do not know the mismatch between MySQL and Python, and other tools, not just ORMs, to deal with the issue, missing in the latter article.
Questions:
How is the problem dealt between MySQL and Python?
Does App Engine's non-SQL makes Python work better together?
Are there some general tools, perhaps ORM, to deal with mismatches?
What are non-standard ways to deal with the problem?
Could you say that the nonSQL is a tool to make the object-oriented world of Python match the Relational world? Or does the new design totally avoid the problem?

ORM is the standard solution for making the object-oriented world of Python match the Relational world of MySQL.
There are at least 3 popular ORM components.
SQLAlchemy
SQLObject
Django's ORM.

As was once said on comp.lang.python ORM's are like morphine -- it can save you pain if you are really hurting, but if you use it regularly you will end up with really big problems.
It's not hard to build relatively low level interfaces between a relational database and an object model. It's extremely hard to migrate an automated ORM mapping to a new design after the fact. Only immature programmers try to simplify things that are not hard without looking ahead to the possible consequences that are extremely hard.
The google app engine mini-rdb-with-some-restrictions-removed is nice because it
only automates extremely simple stuff and forces you to think about the table layout
without pretending that it can all be done automatically.

Related

Python flask/django object-oriented databases usage

I always use SQL or NoSQL databases in my project and at my job, but now I am asked to use an object-oriented DB. I don't even know for what reason I should do that. Despite this fact, I google for OODBMS in python and can't see any easy way to use this approach. Now I think, that django ORM (and flask sql alchemy) are the simplest way to construct databases.
So, I have two questions:
What are the main benefits of using OODBMS instead of, e.x., Django ORM?
Is there a simple way to use OODBMS in flask and django?
For question 1: OODBMS offers many benefits and to mention a few:
It provides greater consistency between the database and the programming language.
Doesn’t bother you with object–relational impedance mismatch.
It is a more expressive query language and it supports long
durations/transactions.
It is also suitable for advanced database applications.
For question 2: ZODB is easier and simpler to use, Django is mostly good with ORM only.

Advantage of Django ORM V/S Performing raw SQL queries [duplicate]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
If you are motivate to the "pros" of an ORM and why would you use an ORM to management/client, what are those reasons would be?
Try and keep one reason per answer so that we can see which one gets voted up as the best reason.
The most important reason to use an ORM is so that you can have a rich, object oriented business model and still be able to store it and write effective queries quickly against a relational database. From my viewpoint, I don't see any real advantages that a good ORM gives you when compared with other generated DAL's other than the advanced types of queries you can write.
One type of query I am thinking of is a polymorphic query. A simple ORM query might select all shapes in your database. You get a collection of shapes back. But each instance is a square, circle or rectangle according to its discriminator.
Another type of query would be one that eagerly fetches an object and one or more related objects or collections in a single database call. e.g. Each shape object is returned with its vertex and side collections populated.
I'm sorry to disagree with so many others here, but I don't think that code generation is a good enough reason by itself to go with an ORM. You can write or find many good DAL templates for code generators that do not have the conceptual or performance overhead that ORM's do.
Or, if you think that you don't need to know how to write good SQL to use an ORM, again, I disagree. It might be true that from the perspective of writing single queries, relying on an ORM is easier. But, with ORM's it is far too easy to create poor performing routines when developers don't understand how their queries work with the ORM and the SQL they translate into.
Having a data layer that works against multiple databases can be a benefit. It's not one that I have had to rely on that often though.
In the end, I have to reiterate that in my experience, if you are not using the more advanced query features of your ORM, there are other options that solve the remaining problems with less learning and fewer CPU cycles.
Oh yeah, some developers do find working with ORM's to be fun so ORM's are also good from the keep-your-developers-happy perspective. =)
Speeding development. For example, eliminating repetitive code like mapping query result fields to object members and vice-versa.
Making data access more abstract and portable. ORM implementation classes know how to write vendor-specific SQL, so you don't have to.
Supporting OO encapsulation of business rules in your data access layer. You can write (and debug) business rules in your application language of preference, instead of clunky trigger and stored procedure languages.
Generating boilerplate code for basic CRUD operations. Some ORM frameworks can inspect database metadata directly, read metadata mapping files, or use declarative class properties.
You can move to different database software easily because you are developing to an abstraction.
Development happiness, IMO. ORM abstracts away a lot of the bare-metal stuff you have to do in SQL. It keeps your code base simple: fewer source files to manage and schema changes don't require hours of upkeep.
I'm currently using an ORM and it has sped up my development.
So that your object model and persistence model match.
To minimise duplication of simple SQL queries.
The reason I'm looking into it is to avoid the generated code from VS2005's DAL tools (schema mapping, TableAdapters).
The DAL/BLL i created over a year ago was working fine (for what I had built it for) until someone else started using it to take advantage of some of the generated functions (which I had no idea were there)
It looks like it will provide a much more intuitive and cleaner solution than the DAL/BLL solution from http://wwww.asp.net
I was thinking about created my own SQL Command C# DAL code generator, but the ORM looks like a more elegant solution
Abstract the sql away 95% of the time so not everyone on the team needs to know how to write super efficient database specific queries.
I think there are a lot of good points here (portability, ease of development/maintenance, focus on OO business modeling etc), but when trying to convince your client or management, it all boils down to how much money you will save by using an ORM.
Do some estimations for typical tasks (or even larger projects that might be coming up) and you'll (hopefully!) get a few arguments for switching that are hard to ignore.
Compilation and testing of queries.
As the tooling for ORM's improves, it is easier to determine the correctness of your queries faster through compile time errors and tests.
Compiling your queries helps helps developers find errors faster. Right? Right. This compilation is made possible because developers are now writing queries in code using their business objects or models instead of just strings of SQL or SQL like statements.
If using the correct data access patterns in .NET it is easy to unit test your query logic against in memory collections. This speeds the execution of your tests because you don't need to access the database, set up data in the database or even spin up a full blown data context.[EDIT]This isn't as true as I thought it was as unit testing in memory can present difficult challenges to overcome. But I still find these integration tests easier to write than in previous years.[/EDIT]
This is definitely more relevant today than a few years ago when the question was asked, but that may only be the case for Visual Studio and Entity Framework where my experience lies. Plugin your own environment if possible.
.net tiers using code smith templates
http://nettiers.com/default.aspx?AspxAutoDetectCookieSupport=1
Why code something that can be generated just as well.
convince them how much time / money you will save when changes come in and you don't have to rewrite your SQL since the ORM tool will do that for you
I think one cons is that ORM will need some updation in your POJO. mainly related to schema, relation and query. so scenario where you are not suppose to make changes in model objects, might be because it is shared among more that on project or b/w client and server. so in such cases you will need to split it in two levels, which will require additional efforts .
i am an android developer and as you know mobile apps are usually not huge in size, so this additional effort to segregate pure-model and orm-affected-model does not seems worth full.
i understand that question is generic one. but mobile apps are also come inside generic umbrella.

Starting new project: database abstraction in Python, best practice for retaining option of MySQL or PostgreSQL without ORM

I want to retain the flexibility of switching between MySQL and PostgreSQL without the awkwardness of using an ORM - SQL is a fantastic language and i would like to retain it's power without the additional overhead of an ORM.
So...is there a best practice for abstraction the database layer of a Python application to provide the stated flexibility.
Thanks community!
While SQLAlchemy is a great option. There are others. If you find that SQLAlchemy is not to your liking here are some other ORMs that may work out better for you. Some of them are more lightweight, so it may be more what you're looking for.
http://coobs.eu.org/xrecord/ -- From the description sounds like what you may be looking for. It sounds pretty lightweight and just a database abstraction, but seems to be a little outdated.
http://autumn-orm.org/
http://charlesleifer.com/blog/peewee-a-lightweight-python-orm/ -- includes benchmarks that look pretty good for the basic uses that are done.
http://elixir.ematia.de/trac/wiki -- This is built on top of SQLAlchemy, but has the ActiveRecord style syntax. It may be more to your liking.
https://storm.canonical.com/FrontPage -- An ORM used by Canonical.
Have a look at SQLAlchemy. You can use it to execute literal SQL on several RDBMS, including MySQL and PostgreSQL. It wraps the DB-API adapters with a common interface, so they will behave as similarly as possible.
SQLAlchemy also offers programmatic generation of SQL, with or without the included ORM, which you may find very useful.

Is there any python web app framework that provides database abstraction layer for SQL and NoSQL?

Is it even possible to create an abstraction layer that can accommodate relational and non-relational databases? The purpose of this layer is to minimize repetition and allows a web application to use any kind of database by just changing/modifying the code in one place (ie, the abstraction layer). The part that sits on top of the abstraction layer must not need to worry whether the underlying database is relational (SQL) or non-relational (NoSQL) or whatever new kind of database that may come out later in the future.
There's a Summer of Code project going on right now to add non-relational support to Django's ORM. It seems to be going well and chances are good that it will be merged into core in time for Django 1.3.
You could use stock Django and Django-nonrel ( http://www.allbuttonspressed.com/projects/django-nonrel ) together to get a quite unified experience. Some limits apply, read docs carefully though, remembering Spolsky's "All abstractions are leaky".
Yo may also check web2py, they support relational databases and GAE on the core.
Regarding App Engine, all existing attempts limit you in some way (web2py doesn't support transactions or namespaces and probably many other stuff, for example). If you plan to work with GAE, use what GAE provides and forget looking for a SQL-NoSQL holy grail. Existing solutions are inevitably limited and affect performance negatively.
Thank you for all the answers. To summarize the answers, currently only web2py and Django supports this kind of abstraction.
It is not about a SQL-NoSQL holy grail, using abstraction can make the apps more flexible. Lets assume that you started a project using NoSQL, and then later on you need to switch over to SQL. It is desirable that you only make changes to the codes in a few spots instead of all over the place. For some cases, it does not really matter whether you store the data in a relational or non-relational db. For example, storing user profiles, text content for dynamic page, or blog entries.
I know there must be a trade off by using the abstraction, but my question is more about the existing solution or technical insight, instead of the consequences.

Scaffolding in pylons

Is there anything similar to rails' scaffolding for pylons? I've been poking around google, but only found this thing called dbsprockets, which is fine, although probably way too much for my needs. What I really need is a basic CRUD that is based on the SQLAlchemy model.
The question is super old, but hell: http://code.google.com/p/formalchemy/
Gives you basic crud out of the box, customizable to do even relatively complex things easily, and gives you a drop-in Pylons admin app too (written and customizable with the same api, no magic).
I hear you, I've followed the Pylons mailing list for a while looking for something similar. There have been some attempts in the past (see AdminPylon and Restin) but none have really kept up with SQLAlchemy's rapidly developing orm api.
Since DBSprockets is likely to be incorporated into TurboGears it will likely be maintained. I'd bite the bullet and go with that.
Just updating an old question. DBSprockets has been replaced by sprox which learns a lot of lessons from it and is pretty cool.
It isn't quite the throwaway 'scaffolding' that Rails provides, it is more like an agile form generation tool that is highly extensible.

Categories

Resources