class Address(db.Model): __tablename__ = 'address' id = db.Column('id', db.Integer, primary_key=True, autoincrement=True) city = db.Column('city', db.String(64), nullable=True) def to_representation(self): return {'city': self.city}
class Cart(db.Model): __tablename__ = 'cart' cart_id = db.Column('cart_id', db.Integer, primary_key=True, autoincrement=True) cart_uid = db.Column('cart_uid', db.Integer, nullable=False) cart_pid = db.Column('cart_pid', db.Integer, nullable=False) cart_qty = db.Column('cart_qty', db.Integer, nullable=False, default=1)
class Payment(db.Model): __tablename__ = 'payment' payment_id = db.Column('payment_id', db.Integer, primary_key=True, autoincrement=True) payment_mode = db.Column('payment_mode', db.String(64), unique=True) prod_id = db.Column('prod_id', db.ForeignKey('products.prod_id'), nullable=False)
class UserDetails(db.Model): __tablename__ = 'user_details' id = db.Column('id', db.Integer, primary_key=True, autoincrement=True) name = db.Column('name', db.String(64), nullable=True) user_id = db.Column('user_id', db.Integer, db.ForeignKey('user.id'), nullable=False) def to_representation(self): return {'name': self.name}
class Category(db.Model): __tablename__ = 'category' cat_id = db.Column('cat_id', db.Integer, primary_key=True, autoincrement=True) cat_name = db.Column('cat_name', db.String(64), index=True, unique=True) Subcategorys = db.relationship('Subcategory', backref=db.backref('category'), uselist=False, lazy='select', cascade="all,delete")
class OrderDetails(db.Model): __tablename__ = 'orderdetails' id = db.Column('orderdet_id', db.Integer, primary_key=True, autoincrement=True) order_id = db.Column('order_id', db.ForeignKey('orders.order_id'), nullable=False) prod_id = db.Column('prod_id', db.ForeignKey('products.prod_id'), nullable=False)
class Subcategory(db.Model): __tablename__ = 'subcategory' subcat_id = db.Column('subcat_id', db.Integer, primary_key=True, autoincrement=True) cat_id = db.Column('cat_id', db.ForeignKey('category.cat_id'), nullable=False) subcat_name = db.Column('subcat_name', db.String(120), unique=True) product = db.relationship('Products', backref=db.backref('subcategory'), uselist=False, lazy='select', cascade="all,delete")
class Vendor(db.Model): __tablename__ = 'vendor' vendor_id = db.Column('vendor_id', db.Integer, primary_key=True, autoincrement=True) firstname = db.Column('firstname', db.String(64)) lastname = db.Column('lastname', db.String(64)) email = db.Column('email', db.String(120), unique=True) password = db.Column('password', db.String(128)) address = db.Column('address', db.String(200)) phone_no = db.Column('phone_no', db.String(10)) pincode = db.Column('pincode', db.String(6))
class Products(db.Model): __tablename__ = 'products' prod_id = db.Column('prod_id', db.Integer, primary_key=True, autoincrement=True) subcat_id = db.Column('subcat_id', db.ForeignKey('subcategory.subcat_id'), nullable=True) prod_name = db.Column('prod_name', db.String(120), index=True, unique=True) prod_desc = db.Column('prod_desc', db.String(120)) prod_price = db.Column('prod_price', db.Integer) prod_qty = db.Column('prod_qty', db.Integer) prod_image = db.Column('prod_image', db.String(120)) prod_brand = db.Column('prod_brand', db.String(80)) #prod_carts = db.relationship('Cart', backref=db.backref('products'), uselist=False, lazy='select', cascade="all,delete") #orders = db.relationship('Order', backref='products', uselist=False, cascade="all,delete") payments = db.relationship('Payment', backref='products', uselist=False, lazy='select', cascade="all,delete") orderdet = db.relationship('OrderDetails', backref='products')
class User(db.Model): __tablename__ = 'user' id = db.Column('id', db.Integer, primary_key=True, autoincrement=True) username = db.Column('username', db.String(64), index=True, unique=True) email = db.Column('email', db.String(120), unique=True) password = db.Column('password', db.String(128)) user_detail = db.relationship( 'UserDetails', backref='user', lazy='select', uselist=False, cascade="all,delete", ) # one-to-one relationship addresses = db.relationship( 'Address', secondary=users_addresses, backref='user', lazy='joined' #use 'dynamic' and check the difference , cascade="all,delete") # Many-to-many Relationship def to_representation(self): if self.user_detail: user_detail = self.user_detail.to_representation() else: user_detail = {} addresses = [x.to_representation() for x in self.addresses] return { 'username': self.username, 'email': self.email, 'password': self.password, 'user_detail': user_detail, 'addresses': addresses } def generate_auth_token(self): s = URLSafeSerializer(app.config['SECRET_KEY']) return s.dumps({'id': self.id})
class User(db.Model): __tablename__ = 'user' user_id = db.Column('user_id', db.Integer, primary_key=True, autoincrement=True) username = db.Column('username', db.String(64), index=True, unique=True) email = db.Column('email', db.String(120), unique=True) password = db.Column('password', db.String(128)) address = db.Column('address', db.String(200)) phone_no = db.Column('phone_no', db.String(10)) pincode = db.Column('pincode', db.String(16)) #prod_carts = db.relationship('Cart', backref='user', uselist=False, lazy='select', cascade="all,delete") orders = db.relationship('Order', backref='user', uselist=True, lazy='joined', cascade="all,delete")
class Order(db.Model): __tablename__ = 'orders' order_id = db.Column('order_id', db.Integer, primary_key=True, autoincrement=True) user_id = db.Column('user_id', db.ForeignKey('user.user_id'), nullable=False) #payment_id = db.Column('payment_id', db.String()) firstname = db.Column('firstname', db.String(64)) lastname = db.Column('lastname', db.String(64)) email = db.Column('email', db.String(120), unique=True) address = db.Column('address', db.String(200)) city = db.Column('city', db.String(200)) country = db.Column('country', db.String(200)) phone_no = db.Column('phone_no', db.String(10)) pincode = db.Column('pincode', db.String(6)) payment_mode = db.Column('payment_mode', db.String(64)) dttm = db.Column(db.DateTime, default=datetime.datetime.utcnow) orderdet = db.relationship('OrderDetails', backref='order') ''' prod_name = db.Column('prod_name', db.String(120), index=True, unique=True)
return another query object which you can further refine before loading the items. This is usually what you want if you expect more than a handful of items for this relationship. "Custom Join query:" User.query.join( Address, (User.id == Address.user.id)).filter(<condition>) OR all() """ from itsdangerous import URLSafeSerializer from flask_ecommerce import db, auth from flask import current_app as app, g users_addresses = db.Table( 'users_addresses', db.Column('user_id', db.Integer, db.ForeignKey('user.id'), primary_key=True), db.Column('address_id', db.Integer, db.ForeignKey('address.id'), primary_key=True)) class User(db.Model): __tablename__ = 'user' id = db.Column('id', db.Integer, primary_key=True, autoincrement=True) username = db.Column('username', db.String(64), index=True, unique=True) email = db.Column('email', db.String(120), unique=True) password = db.Column('password', db.String(128)) user_detail = db.relationship( 'UserDetails',