-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQL.py
More file actions
32 lines (23 loc) · 1.23 KB
/
SQL.py
File metadata and controls
32 lines (23 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from sqlalchemy import create_engine, Column, String, Integer, Text, DateTime, ForeignKey
from sqlalchemy.orm import DeclarativeBase, sessionmaker
from datetime import datetime
engine = create_engine('sqlite:///Diary.db')
SessionLocal = sessionmaker(bind=engine)
class Base(DeclarativeBase):
pass
class User(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True)
name = Column(String)
password = Column(String, nullable=False) # nullable - параметр об отсутствии данных
class Record(Base):
__tablename__ = "records"
id = Column(Integer, primary_key=True, autoincrement=True)
title = Column(String(100)) # Заголовок
content = Column(Text)
# Передаем саму функцию utcnow БЕЗ скобок, чтобы она вызывалась в момент создания записи
created_at = Column(DateTime, default=datetime.now) # Автоматическая фиксация времени создания.
# Указываем 'имя_таблицы.имя_колонки'
user_id = Column(Integer, ForeignKey('users.id'), nullable=False)
def start_db():
Base.metadata.create_all(engine)