Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically reflect database to sqlalchemy declarative?

sqlautocode - has issues with many-to-many relations

sqlsoup - doesn't support relations

elixir - it's note auto-generate

Is there something else I could try?

like image 353
mdob Avatar asked Jun 09 '11 08:06

mdob


People also ask

What is Declarative in SQLAlchemy?

The Declarative system is the typically used system provided by the SQLAlchemy ORM in order to define classes mapped to relational database tables. However, as noted in Classical Mappings, Declarative is in fact a series of extensions that ride on top of the SQLAlchemy mapper() construct.

What is SQLAlchemy reflection?

SQLAlchemy can be used to automatically load tables from a database using something called reflection. Reflection is the process of reading the database and building the metadata based on that information. It's the opposite of creating a Table by hand and is very useful for working with existing databases.

What is SQLAlchemy ext Declarative?

function sqlalchemy.ext.declarative. has_inherited_table(cls) Given a class, return True if any of the classes it inherits from has a mapped table, otherwise return False. This is used in declarative mixins to build attributes that behave differently for the base class vs. a subclass in an inheritance hierarchy.

How does SQLAlchemy update data?

Update table elements in SQLAlchemy. Get the books to table from the Metadata object initialized while connecting to the database. Pass the update query to the execute() function and get all the results using fetchall() function. Use a for loop to iterate through the results.


1 Answers

In theory reflection in sqlalchemy should work for you. In this case I'm using an mssql database with two tables which have a simple Many-to-one relation:

"Tests" with fields:

  • id
  • testname
  • author_id (foreign key to the Users table, Users.id field)

"Users" with fields:

  • id
  • fullname

So the following should reflect the database:

from sqlalchemy import * from sqlalchemy.orm import create_session from sqlalchemy.schema import Table, MetaData from sqlalchemy.ext.declarative import declarative_base  #Create and engine and get the metadata Base = declarative_base() engine = create_engine('put your database connect string here') metadata = MetaData(bind=engine)  #Reflect each database table we need to use, using metadata class Tests(Base):     __table__ = Table('Tests', metadata, autoload=True)  class Users(Base):     __table__ = Table('Users', metadata, autoload=True)  #Create a session to use the tables     session = create_session(bind=engine)  #Here I will just query some data using my foreign key relation,  as you would #normally do if you had created a declarative data mode. #Note that not all test records have an author so I need to accomodate for Null records testlist = session.query(Tests).all()      for test in testlist:     testauthor = session.query(Users).filter_by(id=test.author_id).first()       if not testauthor:         print "Test Name: {}, No author recorded".format(test.testname)     else:         print "Test Name: {}, Test Author: {}".format(test.testname, testauthor.fullname) 

So this appears to work with table relations. Although you still haven't given much detail to exactly what you are trying to do.

like image 100
Raceyman Avatar answered Sep 28 '22 07:09

Raceyman