Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLAlchemy - what is declarative_base

I am learning sqlalchemy.
Here is my initial code :

user.py

from sqlalchemy import Column, Integer, Sequence, String from sqlalchemy.ext.declarative import declarative_base Base = declarative_base()  class User(Base):    __tablename__ = 'users'    id = Column(Integer,Sequence('user_seq'), primary_key=True)    username = Column(String(50), unique=True)    fullname = Column(String(150))    password = Column(String(50))    def __init__(self, name, fullname, password):       self.name = name       self.fullname = fullname       self.password = password 

main.py

from sqlalchemy.orm import sessionmaker from sqlalchemy import create_engine from user import User from sqlalchemy.ext.declarative import declarative_base Base = declarative_base()  if __name__ == '__main__':    engine = create_engine('mysql://root:[email protected]:3306/test', echo=True)    Base.metadata.create_all(engine, checkfirst=True)    Session = sessionmaker(bind=engine)    session = Session()    ed_user = User('ed', 'Ed Jones', 'edspassword')    session.add(ed_user)    session.commit() 

When I run main.py, it won't create tables automatically and gives me an exception on session.commit().

Also, when I move line Base = declarative_base() to any different module and use the same Base variable in main.py and in user.py - it creates the table.

My question: "What is declarative_base" ?

like image 795
Aniruddha Avatar asked Mar 02 '13 14:03

Aniruddha


People also ask

What is a declarative class?

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 declarative table?

A declarative table configuration allows the addition of new Column objects to an existing mapping after the Table metadata has already been generated.

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.

What does base MetaData Create_all do?

create_all() creates foreign key constraints between tables usually inline with the table definition itself, and for this reason it also generates the tables in order of their dependency.


1 Answers

declarative_base() is a factory function that constructs a base class for declarative class definitions (which is assigned to the Base variable in your example). The one you created in user.py is associated with the User model, while the other one (in main.py) is a different class and doesn't know anything about your models, that's why the Base.metadata.create_all() call didn't create the table. You need to import Base from user.py

from user import User, Base 

instead of creating a new Base class in main.py.

like image 173
Audrius Kažukauskas Avatar answered Sep 24 '22 12:09

Audrius Kažukauskas