orm

How to get all objects with certain interval of time in django?

How to get all objects with certain interval of time in django? Question: I am attempting to retrieve all objects with a time-to-live of less than 5 seconds using Django’s ORM in Python. However, my current approach is not producing the expected results. Can you help me understand what I am doing wrong and how …

Total answers: 1

AttributeError: 'scoped_session' object has no attribute 'session'

AttributeError: 'scoped_session' object has no attribute 'session' Question: I am trying to insert new data to my database but my web application returns this error. My codes are as follows: models.py import os from flask import Flask from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy() … class Patient(db.Model): __tablename__ = “patients” id = db.Column(db.Integer, primary_key=True) lname …

Total answers: 3

How to keep VARCHAR in the DB (MYSQL), but ENUM in the sqlalchemy model

How to keep VARCHAR in the DB (MYSQL), but ENUM in the sqlalchemy model Question: I want to add a new Int column to my MYSQL DB, so that in the sqlalchemy ORM it will be converted to an ENUM. For example, let’s say I have this enum: class employee_type(Enum): Full_time = 1 Part_time = …

Total answers: 2

How to make an Inner Join in django?

How to make an Inner Join in django? Question: I want to show in an Html the name of the city, state, and country of a publication. But they are in different tables. Here is my models.py class country(models.Model): country_name = models.CharField(max_length=200, null=True) country_subdomain = models.CharField(max_length=3, null=True) def __str__(self): return self.country_name class countrystate(models.Model): state_name = …

Total answers: 5

Peewee ORM JSONField for MySQL

Peewee ORM JSONField for MySQL Question: I have a peewee model like so: class User(peewee.Model): name = peewee.CharField(unique=True) some_json_data = peewee.CharField() requested_at = peewee.DateTimeField(default=datetime.now()) I know that peewee doesn’t support a JSONField for a MySQL DB, but anyway, I though if I could just convert it to a string format and save to db, I …

Total answers: 2

SQLAlchemy: How do you delete multiple rows without querying

SQLAlchemy: How do you delete multiple rows without querying Question: I have a table that has millions of rows. I want to delete multiple rows via an in clause. However, using the code: session.query(Users).filter(Users.id.in_(subquery….)).delete() The above code will query the results, and then execute the delete. I don’t want to do that. I want speed. …

Total answers: 4

Django Left Outer Join

Django Left Outer Join Question: I have a website where users can see a list of movies, and create reviews for them. The user should be able to see the list of all the movies. Additionally, IF they have reviewed the movie, they should be able to see the score that they gave it. If …

Total answers: 7

sqlalchemy: create relations but without foreign key constraint in db?

sqlalchemy: create relations but without foreign key constraint in db? Question: Since sqlalchemy.orm.relationship() already implies the relation, and I do not want to create a constraint in db. What should I do? Currently I manually remove these constraints after alembic migrations. Asked By: est || Source Answers: Instead of defining “schema” level ForeignKey constraints create …

Total answers: 1

SQLAlchemy: engine, connection and session difference

SQLAlchemy: engine, connection and session difference Question: I use SQLAlchemy and there are at least three entities: engine, session and connection, which have execute method, so if I e.g. want to select all records from table I can do this engine.execute(select([table])).fetchall() and this connection.execute(select([table])).fetchall() and even this session.execute(select([table])).fetchall() – the results will be the same. …

Total answers: 3