-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
62 lines (52 loc) · 1.62 KB
/
models.py
File metadata and controls
62 lines (52 loc) · 1.62 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from db import get_db_connection
def crear_tablas():
conn = get_db_connection()
cur = conn.cursor()
# Tabla de productos con stock
cur.execute("""
CREATE TABLE IF NOT EXISTS productos (
id SERIAL PRIMARY KEY,
nombre TEXT NOT NULL,
precio DECIMAL(10,2) NOT NULL,
stock INT NOT NULL DEFAULT 0
);
""")
# Tabla de ventas
cur.execute("""
CREATE TABLE IF NOT EXISTS ventas (
id SERIAL PRIMARY KEY,
fecha TIMESTAMP DEFAULT current_timestamp,
total DECIMAL(10,2) NOT NULL
);
""")
# Tabla de detalles de venta
cur.execute("""
CREATE TABLE IF NOT EXISTS detalles_venta (
id SERIAL PRIMARY KEY,
venta_id INT REFERENCES ventas(id) ON DELETE CASCADE,
producto_id INT REFERENCES productos(id),
cantidad INT NOT NULL,
subtotal DECIMAL(10,2) NOT NULL
);
""")
conn.commit()
cur.close()
conn.close()
print("Tablas creadas o ya existían.")
def insertar_producto():
conn = get_db_connection()
cur = conn.cursor()
# Insertar un producto de prueba
cur.execute("""
INSERT INTO productos (nombre, precio, stock)
VALUES (%s, %s, %s)
RETURNING id;
""", ("Monitor 25 pulgadas", 620.75, 10))
producto_id = cur.fetchone()[0] # Obtener el ID del producto insertado
conn.commit()
print(f"Producto de prueba insertado con ID: {producto_id}")
cur.close()
conn.close()
if __name__ == "__main__":
crear_tablas()
insertar_producto()