Python SQLAlchemy: help describe the tables



  • Comments on models indicate what communication is required


    P.s. I want to see the solution that I understand how it works in his analysis because I don't understand the documentation.

    
    class Product(Base):
        """
    
    id -- primary int key
    name -- string column with max 64 chars
    article -- unique shop article int column
    items -- relation to ReceiptItems
    """
    __tablename__ = 'product'
    id = ...
    name = ...
    article = ...
    items = ...
    

    class ReceiptItem(Base):
    """
    Corresponds to a line in a receipt

    id -- primary key int column
    receipt_id -- receipt foreign key column
    receipt -- relation to Receipt
    product_id -- product foreign key column
    product -- relation to Product
    quantity -- quantity of sold product in ReceiptItem
    price -- price of sold product in ReceiptItem
    
    Constraints:
      - Receipt MUST NOT contain two ReceiptItems for same product
    """
    __tablename__ = 'receipt_item'
    
    id = ...
    receipt_id = ...
    receipt = ...
    product_id = ...
    product = ...
    quantity = ...
    price = ...
    

    class Receipt(Base):
    """

    id -- primary key  int column
    completed_at -- datetime column
    items -- relation to ReceiptItems
    """
    __tablename__ = 'receipt'
    id = ...
    completed_at = ...
    items = ...
    



  • It's an exemplary structure. The rest of the connections are on your own.

    MyApp = Flask(__name__)
    MyApp.config['SQLALCHEMY_DATABASE_URI'] = 'ИМЯ_СУБД://ИМЯ_ПОЛЬЗОВАТЕЛЯ:ПАРОЛЬ_БД$$@IP_АДРЕС_СЕРВЕРА:ПОРТ/ИМЯ_БД'
    MyApp.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
    db = SQLAlchemy(MyApp)
    db.init_app(MyApp)
    

    class ReceiptItem(db.Model):

    __tablename__ = 'receipt_item'
    id = db.Column(db.Integer, primary_key=True)
    receipt_id = ...
    receipt =  db.Column(db.Integer, db.ForeignKey('receipt.id'))
    product_id = db.Column(db.Integer, db.ForeignKey('product.id'))
    product = ...
    quantity = ...
    price = ...
    

    class Product(db.Model):
    tablename = 'product'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.Text, unique=False, nullable=False)
    article = db.Column(db.Text, unique=False, nullable=False)
    items = db.Column(db.Integer, db.ForeignKey('receipt_item.id'))

    def __repr__(self):
        return f"{self.id},{self.name},{self.article},{self.items}"
    

    class Receipt(db.Model):
    """

    id -- primary key  int column
    completed_at -- datetime column
    items -- relation to ReceiptItems
    """
    __tablename__ = 'receipt'
    id = ...
    completed_at = ...
    items = ...
    

    db.create_all()



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2