How Many Different Types of Columns Are Available in SQLAlchamey?

  • Read
  • Discuss

This tutorial provides a comprehensive overview of the various column types available in SQLAlchemy and the attributes associated with them. SQLAlchemy is a popular SQL toolkit and Object Relational Mapper (ORM) library for Python. It allows developers to interact with databases using Python code instead of writing raw SQL. In this tutorial, you will learn about the different types of columns available in SQLAlchemy, including Integer, Float, String, Text, DateTime, Boolean, and Enum, along with their associated attributes such as primary_key, autoincrement, nullable, precision, asdecimal, collation, length, default, and enum. By the end of this tutorial, you will have a good understanding of how to define columns in SQLAlchemy and use their attributes to customize the behavior of your database.

Column Types Available With Multiple Attributes

SQLAlchemy has roughly 7 column types with multiple attributes associated with them. The complete details are provided in the table below. The details contain a brief explanation along with attributes associated with them and sample code as well.

Serial No.Column TypeExplanationAttributes AssociatedSample Code
1IntegerRepresents an integer data type in the database primary_key: indicates that the column is a primary keyautoincrement: indicates that the values in this column should automatically incrementnullable: indicates whether the column can have a null valueimport sqlalchemy as dbengine = db.create_engine(‘sqlite:///test.sqlite’) connection = engine.connect()metadata = db.MetaData()

test = db.Table(‘test’, metadata,              db.Column(‘id’, db.Integer(), primary_key=True, autoincrement=True, nullable=False)              )metadata.create_all(engine)
2FloatRepresents a floating-point data type in the database.precision: the number of decimal places to storeasdecimal: indicates whether the value should be stored as a decimal or a floatimport sqlalchemy as db
engine = db.create_engine(‘sqlite:///test.sqlite’) connection = engine.connect()
metadata = db.MetaData()


test = db.Table(‘test’, metadata,
              db.Column(‘price’, db.Float(), precision=2,asdecimal=True)
              )
metadata.create_all(engine)
3StringRepresents a character string data type in the database.length: the maximum length of the stringcollation: the collation to use for comparing stringsnullable: indicates whether the column can have a null value
import sqlalchemy as db
engine = db.create_engine(‘sqlite:///user.sqlite’)
connection = engine.connect()
metadata = db.MetaData()

user = db.Table(‘user’, metadata,
           
              db.Column(‘username’, db.String(255), nullable=False,collation=’utf8mb4_general_ci’)
              )

metadata.create_all(engine)
4TextRepresents a large text data type in the database.collation: the collation to use for comparing stringsimport sqlalchemy as db
engine = db.create_engine(‘sqlite:///test.sqlite’) connection = engine.connect()
metadata = db.MetaData()


test = db.Table(‘test’, metadata,
              db.Column(‘description’, db.Text(), collation=’utf8mb4_General_ci’)
              )
metadata.create_all(engine)
5DateTimeRepresents a date and time data type in the database.default: the default value to use when a value is not providedimport sqlalchemy as db
engine = db.create_engine(‘sqlite:///test.sqlite’) connection = engine.connect()
metadata = db.MetaData()


test = db.Table(‘test’, metadata,
              db.Column(‘created_at’, db.DateTime(), default=datetime.utcnow)
              )
metadata.create_all(engine)
6BooleanRepresents a boolean data type(True/False) in the database.default: the default value to use when a value is not providedimport sqlalchemy as db
engine = db.create_engine(‘sqlite:///test.sqlite’) connection = engine.connect()
metadata = db.MetaData()


test = db.Table(‘test’, metadata,
              db.Column(‘active’, db.Boolean(), default=True)
              )
metadata.create_all(engine)
7EnumRepresents an enumerated data type in the database.enum: the enumerated type to useimport sqlalchemy as db
engine = db.create_engine(‘sqlite:///test.sqlite’) connection = engine.connect()
metadata = db.MetaData()


test = db.Table(‘test’, metadata,
              db.Column(‘status’, db.Enum(“active”, “inactive”, “deleted”))
              )
metadata.create_all(engine)

Conclusion

In conclusion, SQLAlchemy provides a wide range of column types for developers to choose from when defining tables in their database. By using the attributes associated with each column type, you can customize the behavior of your database and ensure that it meets your specific requirements. Whether you are creating a database from scratch or working with an existing one, understanding the different column types and their attributes is a critical skill for any SQLAlchemy developer. By following this tutorial, you should now have a solid foundation in working with columns in SQLAlchemy and be well on your way to building robust and efficient database systems.

Leave a Reply

Leave a Reply

Scroll to Top