SQLAlchemy introspection

What I'm trying to do is to get all of its Column () value from the SqlAlchemy entity definition, determine their types and restrictions so that they can pre-validate, convert data and display user forms for the user.

How do I understand that?

Example:

class Person(Base): ''' Represents Person ''' __tablename__ = 'person' # Columns id = Column(String(8), primary_key=True, default=uid_gen) title = Column(String(512), nullable=False) birth_date = Column(DateTime, nullable=False) 

I want to get this identifier, title, date of birth, determine their limitations (for example, the name is a string, and the maximum length is 512 or the date of birth is a date, etc.).

thanks

+3
source share
1 answer

If you are using sqlalchemy 0.8 , you should check the new function of the Verification System new class . An example of extracting code from the documentation:

 class User(Base): __tablename__ = 'user' id = Column(Integer, primary_key=True) name = Column(String) name_syn = synonym(name) addresses = relationship(Address) # universal entry point is inspect() >>> b = inspect(User) # column collection >>> b.columns [<id column>, <name column>] 

Otherwise, see Access to tables and columns in the documentation. Retrieving the code from the document again:

 employees = Table(...) # or if using declarative #employees = Employee.__table__ # or just employees.c.employee_id # via string employees.c['employee_id'] # iterate through all columns for c in employees.c: print c # access a column name, type, nullable, primary key, foreign key employees.c.employee_id.name employees.c.employee_id.type employees.c.employee_id.nullable employees.c.employee_id.primary_key employees.c.employee_dept.foreign_keys 
+6
source

Source: https://habr.com/ru/post/1448069/


All Articles