In the SQLAlchemy ORM tutorial the following code is given as an example of a class which will be mapped to a table:
>>> from sqlalchemy import Column, Integer, String
>>> class User(Base):
... __tablename__ = 'users'
...
... id = Column(Integer, primary_key=True)
... name = Column(String)
... fullname = Column(String)
... password = Column(String)
...
... def __init__(self, name, fullname, password):
... self.name = name
... self.fullname = fullname
... self.password = password
...
... def __repr__(self):
... return "<User('%s','%s', '%s')>" % (self.name, self.fullname, self.password)
If name
, fullname
and password
are set by the user in the __init__
method when the class is instantiated, what's the point of having them declared as Column
objects (i.e as class variables)? I don't understand how and when SQLAlchemy is able to use the information - is it somehow passed to the SQLAlchemy module via the 'Base' class which User
is inheriting from? (I didn't think it was possible to pass information to a unit in this way - by declaring a class which inherits from another class).