Semaine 7
This commit is contained in:
105
Semaine_07/Jour_05/orm_peewee/relations.py
Normal file
105
Semaine_07/Jour_05/orm_peewee/relations.py
Normal file
@@ -0,0 +1,105 @@
|
||||
from peewee import *
|
||||
|
||||
# import os
|
||||
# from dotenv import load_dotenv
|
||||
|
||||
# load_dotenv()
|
||||
|
||||
# db_name = os.environ.get("DB_NAME")
|
||||
# db_user = os.environ.get("DB_USER")
|
||||
# db_password = os.environ.get("DB_PASSWORD")
|
||||
# db_host = os.environ.get("DB_HOST")
|
||||
# db_port = os.environ.get("DB_PORT")
|
||||
|
||||
db_name = "company"
|
||||
db_user = "root"
|
||||
db_password = "MementoMarsouin96"
|
||||
db_host = "localhost"
|
||||
db_port = 3306
|
||||
|
||||
db = MySQLDatabase(
|
||||
database= db_name,
|
||||
user= db_user,
|
||||
password= db_password,
|
||||
host= db_host,
|
||||
port= db_port
|
||||
)
|
||||
|
||||
# équivalent de "Base = declarative_base()"
|
||||
class BaseModel(Model):
|
||||
class Meta:
|
||||
database = db
|
||||
|
||||
# Relations One-To-Many
|
||||
class User(BaseModel):
|
||||
username = CharField(unique= True)
|
||||
email = CharField()
|
||||
|
||||
class Meta:
|
||||
table_name = "user"
|
||||
|
||||
class Todo(BaseModel):
|
||||
title = CharField()
|
||||
is_done = BooleanField(default= False)
|
||||
user= ForeignKeyField(User, backref= "todos") # l'accès inverse est géré automatiquement
|
||||
|
||||
class Meta:
|
||||
table_name = "todos"
|
||||
|
||||
class Tag(BaseModel):
|
||||
name = CharField(unique= True)
|
||||
|
||||
class Meta:
|
||||
table_name = "tag"
|
||||
|
||||
class TodoTag(BaseModel):
|
||||
todo = ForeignKeyField(Todo)
|
||||
tag = ForeignKeyField(Tag)
|
||||
|
||||
class Meta:
|
||||
table_name = 'todo_tag'
|
||||
indexes= (
|
||||
(('todo', 'tag'), True),
|
||||
)
|
||||
|
||||
class Profile(BaseModel):
|
||||
user = ForeignKeyField(User, unique= True, backref= "profile") # One-To-One
|
||||
bio = TextField(null= True)
|
||||
avatar = CharField(null= True)
|
||||
|
||||
class Meta:
|
||||
table_name = "profile"
|
||||
|
||||
db.connect()
|
||||
db.create_tables([User, Todo, Profile, Tag, TodoTag])
|
||||
|
||||
try:
|
||||
alice = User.create(username= "Alice", email= "a.smith@company.au")
|
||||
Todo.create(title= "Apprendre Python", user= alice)
|
||||
Todo.create(title= "Apprendre JS", user= alice)
|
||||
Todo.create(title= "Apprendre COBOL", user= alice)
|
||||
|
||||
for todo in alice.todos:
|
||||
print(todo.title, todo.is_done)
|
||||
except IntegrityError as error:
|
||||
print(error)
|
||||
|
||||
try:
|
||||
Profile.create(
|
||||
user= User.get(1),
|
||||
bio = 'Developpeur',
|
||||
avatar= 'http://example.com'
|
||||
)
|
||||
except IntegrityError as error:
|
||||
print(error)
|
||||
|
||||
try:
|
||||
tag_urgent = Tag.create(name= "Urgent")
|
||||
tag_perso = Tag.create(name= "Perso")
|
||||
tag_pro = Tag.create(name= "Pro")
|
||||
|
||||
todo_cobol = Todo.get(Todo.title == "Apprendre COBOL")
|
||||
|
||||
TodoTag.create(todo= todo_cobol, tag= tag_urgent)
|
||||
except IntegrityError as error:
|
||||
print(error)
|
||||
Reference in New Issue
Block a user