db.Model, db.session and defining models

Given an instance of the SQLAlchemy class from Flask-SQLAlchemy,

db = SQLAlchemy(app)
  • db is an interface for interacting with our database
  • db.Model lets us create and manipulate data models. Models which are defined as classes mapped to tables within our database.
  • db.session lets us create and manipulate database transactions. The context of a session which we use in order to interact with the database.

Declaring classes

  • class MyModel(db.Model) will inherit from db.Model
  • By inheriting from db.Model, we map from our classes to tables via SQLAlchemy ORM

Defining columns

  • Within our class, we declare attributes equal to db.Column(…)
  • db.Column takes datatype, primary_key, constraint, default

Table naming

  • By default, SQLAlchemy will pick the name of the table for you, setting it equal to the lower-case version of your class’s name. Otherwise, we set the name of the table using
    tablename = ''my_custom_table_name"

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://username@localhost:5432/example'
db = SQLAlchemy(app)

# The Person class inherit from db.Model, we wound up linking and connecting to SQLAlchemy's mappings between classes and tables.
class Person(db.Model):
    # By defult, SQLAlchemy will pick the name of the table for you and set it equal to the lowercase version of your class.
    # But if you want to control the name of the table, you can do this way
    __tablename__ = 'persons'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(), nullable=False)


@app.route('/')
def index():
    return 'Hello World!'

if __name__ == '__main__':
    app.run()
WARNINGS: ?: (staticfiles.W004) The directory 'static' in the STATICFILES_DIRS setting does not exist. buy.Cart: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'. HINT: Configure the DEFAULT_AUTO_FIELD setting or the BuyConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.d b.models.BigAutoField'. buy.Orders: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'. HINT: Configure the DEFAULT_AUTO_FIELD setting or the BuyConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.d b.models.BigAutoField'. sale.Brand: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'. HINT: Configure the DEFAULT_AUTO_FIELD setting or the SaleConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django. db.models.BigAutoField'. sale.Carinfo: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'. HINT: Configure the DEFAULT_AUTO_FIELD setting or the SaleConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django. db.models.BigAutoField'. userinfo.UserInfo: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'. HINT: Configure the DEFAULT_AUTO_FIELD setting or the UserinfoConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'dja ngo.db.models.BigAutoField'. No changes detected
06-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值