#databases
1 messages · Page 37 of 1
When I try to connect my python mysql database in idle, It shows RESTART: SHELL without any error. Does anyone know how to solve it?
This is website for Python..
what are all the types of apis?
ie i can get a api froma movie website to show the movie dates and names
what else can i do with apis
Definitely, maybe even for free. Just do a web search.
As for other things, these are all free: https://github.com/public-apis/public-apis?tab=readme-ov-file#entertainment
you can write your own to display live updates of your own scripts running.
As a beginner how do i get started with databased
I've heard about the django framework but idk if its beginner friendly/recommended
Django is an entire backend framework, not just a database library
Say if you need to learn SQL, https://sqlbolt.com is a resource that may help
SQLBolt provides a set of interactive lessons and exercises to help you learn SQL
After that there isn't much I can give
There's also flask and fastapi. #web-development would be a good place to ask about those.
Tysm!
Hi,
CREATE TABLE students (
id int,
Firstname varchar (200),
Lastname varchar (200),
Email varchar (100)
);
Insert into students
Values
(1, "Watson", "Keter", "Watsonketer@gmail.com");
It says column "Watson" does not exist.
you need to use single quotes for strings. Double quotes always references to columns and tables.
Can anyone help direct me on how I filter on a relationship in sqlalchemy 2.0?
Need more info
Guys, Do u have info about indexing, query optimization and etc? Mb book/video
I figured out the filtering thing; I realized I was thining about the code wrong. SQLA goes a lot more explicit, so doing the join and filtering on the join was my correct route.
I'm having trouble with a UUID field. I have this right now:
token: Mapped[UUID] = mapped_column(String(36), default=uuid4)
Problem is when I create a new object and save it to the database, it doesn't convert the UUID to string, and I'm not sure how to fix that. I'd figure as the column is a string type, it'd do the conversion internally?
str()
... where?
On every use of the field? Given it's being auto generated, I can't do that
I could remove the default, but then what's the point?
?
When pushing data to MariaDB, has anyone found it's better to push something similar to the datatype for a field on the database? Or just push strings and hope for the best?
hey i need help guys how can i store the info1 and 2 into the columns of abc using panda, im having a really hard time with this
for small to medium scale apps what are the languages used for the backend? same as front end ie python or different?
csv?
just make it manually
https://www.geeksforgeeks.org/working-csv-files-python/
what would be a good way to design a system of workout routines, where each routine has a set of exercises, where each exercise has sets of repetitions but those sets can be different for each routine
(in sql)
You basically designed it yourself there
You add extra info in the join tables between them
So a table of routines and a table for exercises. Join table between routines and exercises contains data on how many sets and reps
I'm again stuck with SQLA basics. I have a field
activated_on: Mapped[datetime.datetime] = mapped_column(nullable=True)
I'm setting it as
self.activated_on = datetime.datetime.now(datetime.timezone.utc)
Yet I'm getting
gamersplane-api | sqlalchemy.exc.DBAPIError: (sqlalchemy.dialects.postgresql.asyncpg.Error) <class 'asyncpg.exceptions.DataError'>: invalid input for query argument $1: datetime.datetime(2024, 12, 15, 23, 7, 4... (can't subtract offset-naive and offset-aware datetimes)
gamersplane-api | [SQL: UPDATE users SET activated_on=$1::TIMESTAMP WITHOUT TIME ZONE WHERE users.id = $2::INTEGER]
gamersplane-api | [parameters: (datetime.datetime(2024, 12, 15, 23, 7, 42, 438459, tzinfo=datetime.timezone.utc), 1)]
gamersplane-api | (Background on this error at: https://sqlalche.me/e/20/dbapi)
as my error? I'm not doing any datetime subtraction here. It looks like the SQL is trying to do tz independent timestamp, whereas I'm using a tz to set mine. Do I need to manually coerse that data? If so, that's stupid as hell.
Explain more? What algorithm would you use to build the workout?
idk just have the user enter their stuff
Or are you just storing the workout history, like logging their weight/sets/reps?
not doing anything too fancy but just a logger
yeah
Have just an exercise table: date, exercise, set #, weight, num_reps
but in routines
i want exercises to have different sets and number of reps per routine
Ok, routine name, exercise, set number, weight, reps
You could have a separate table for each exercise with information about it, like instructions or target muscles
yeah thats it thx
Most things can just be calculated directly off the exercise table, like PRs
yeah
alr so i came up with this
in a program, should i keep opening and closing new connections and cursors, just the cursor, or having both of them open when the program starts and closingm when it ends?
Cursor shouldn't be reused for multiple operations
Having an open connection is fine (if it's sqlite)
And there you shouldn't depend on Routine Name since it's something that can change, it would be better to use a surrogate id (autoincrementing integer, UUID, etc)
yeah i just thought about that
but how do i do that relation between the exercises column of routines and the exercises
is there something specific i have to do in the table creation?
Not really, but you in most (99% of) cases create a foreign key
in your case excercises table would have a foreign key to routines.id
what is a foreign key though
That's something that you could look up by yourself, I don't think I'm that good at explaining stuff 😉 but tldr it helps to keep database consistent, e.g. if you use an excercise_id that doesn't exist it would throw an error
cursor.execute(
"""CREATE TABLE IF NOT EXISTS exercises (
routine_id INTEGER PRIMARY KEY,
name TEXT,
date_modified INTEGER,
note TEXT
)"""
)
cursor.execute(
"""CREATE TABLE IF NOT EXISTS routines (
routine_id INTEGER PRIMARY KEY,
name TEXT,
date_modified INTEGER,
note TEXT,
FOREIGN KEY (exercises)
REFERENCES exercises (routine_id)
)"""
)```
would this be enough?
Maybe like this?
CREATE TABLE IF NOT EXISTS exercises (
id INTEGER PRIMARY KEY, -- Change routine_id to just id, also should have been "excercise_id" if you want to follow that naming scheme
name TEXT,
date_modified INTEGER,
note TEXT
);
CREATE TABLE IF NOT EXISTS routines (
id INTEGER PRIMARY KEY, -- Can be just id
excercise_id INTEGER NOT NULL,
name TEXT,
date_modified INTEGER,
note TEXT,
FOREIGN KEY (excercise_id) -- We reference `excercises.id` by `excercise_id` column
REFERENCES exercises (id)
);
Also would be a good idea to sprinkle not null everywhere, columns in sql are nullable by default
ok
Regarding naming - there's no need to name columns/fields as entity_id when you already know they belong to entity
e.g.
class User:
id: int
would be better than
class User:
user_id: int
to reference the excercise?
if you use your primary key for that then you'd only be able to have one excercise per routine, since primary keys are unique
so i may have forgotten that each exercise needs to have a number of sets and repetitions
but they should be custom to each routine
Hey everyone! Would setting up my own home server out of an old pc be a good learning experience as an aspiring backend dev? Or would it be better to allocate my time elsewhere?
In a routine, each exercise may have multiple sets. And each set may have different reps and weights. For instance, I'll do warmup sets of squats at 10 reps, and then my working sets are 5 reps. Sometimes I'll do a drop set where a single set has multiple weights
Broadly: the question is whether you're building a logger or a planner. You originally said a workout logger, but your schema looks more like a planner.
(Or perhaps a planner + logger)
psql/pg_dump(and psycopg2 if using not binary) versions
fall apart for us with error
/usr/lib/postgresql/16/bin/pg_dump: error while loading shared libraries: libpq.so.5: cannot open shared object file: No such file or directory
if installing postgres-client-16 in python:3.11-bullseye docker image.
FROM public.ecr.aws/docker/library/python:3.11-bullseye AS builder
# Setup working directory
RUN mkdir /code
WORKDIR /code
RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ bullseye-pgdg main" > /etc/apt/sources.list.d/pgdg.list
RUN curl -s https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -
RUN apt-get update \
&& apt-get install -y \
postgresql-client-16 \
&& rm -r /var/lib/apt/lists/*
They have the same libpq appears randomly on psycopg import or pg_dump activation. perhaps someone has ideas how to resolve it or how to google some place which has information about it
error is reproducable if just trying pg_dump -h db -U postgres to dump default postgres within this docker image
May be there is an option to install postgres-client-16 to python image in another way 🤔
SQLITE_CORRUPT: sqlite3 result code 11: database disk image is malformed
first time in my life I see this in the vscode sqlite viewer extension
Im writing an SQL lexer as a elarning project, does this belong to this category or should I cdiscuss it somewhere else?
Anyone know if there's a way to connect to a postgres db host and drop the only database on there with asyncpg? I'm trying to connect as
conn = await asyncpg.connect(
host=DATABASE_HOST,
user=DATABASE_USER,
password=DATABASE_PASSWORD,
)
but it keeps saying it can't drop the currently open database.
usually all postgres dbs have postgres system db
connect to it for dropping others.
I'm also trying DROP OWNED BY {DATABASE_USER}, but then I get
cannot drop objects owned by role gamersplane because they are required by the database system
Hm... this is a docker container running postgres, I'll look into if I have access to an postgres user.
Oh, not user, you mean db, got it
I'll try that
Actually no, this one doesn't
bah, will I have to drop the volume... that'll be annoying and a lot slower...
Ok, onto a sqlalchemy thing...
Anyone know what's going on here? I'm not sure what I've missed. I have a script to populate my local db with test data. I'm getting as far as
user = await register_user(
email="contact@gamersplane.com", username="Keleth", password="test1234"
)
user.activate()
db_session.add(user)
await db_session.commit()
admin_role = Role(name="Admin", owner_id=user.id)
At which point it throws an error can't set attribute because of name, which is a @hybrid_property. It's defined as
_name: Mapped[str] = mapped_column("name", String(64), unique=True)
_plural: Mapped[str] = mapped_column("plural", String(64), unique=True)
With the methods themselves being
@hybrid_property
def name(self):
return self._name
@name.setter
def set_name(self, value):
self._name = value
self._plural = pluralize(self._name)
Can anyone spot what I've done wrong?
Can you share the full error?
Sorry, I didn't quote that correctly. The entire error is can't set attribute, with that capitalization.
Uhh, let me try to reproduce it
Hm, why are you using hybrid property here?
Hm, nvm
Rename set_name to name
https://docs.python.org/3/library/functions.html#property
property.setter/getter/deleter returns copy of said property
so your function name should match
otherwise you'd have two different properties, in your case it looks like this:
name = property(fget=your_getter)
set_name = property(fget=your_getter, fset=your_setter)
Btw, are you using MappedAsDataclass or not? It's a bit finnicky to set up with properties, but:
class Book(MappedAsDataclass, Base):
__tablename__ = "book"
id: Mapped[int] = mapped_column(primary_key=True, init=False)
_name: Mapped[str] = mapped_column("name", String(64), unique=True, init=False)
_plural: Mapped[str] = mapped_column("plural", String(64), unique=True, init=False)
name: Mapped[str]
@hybrid_property
def name(self):
return self._name
@name.setter
def name(self, value):
self._name = value
self._plural = value * 2
Same seems to apply with standard dataclasses too (you need to define your annotation/field separately from the property):
@dataclasses.dataclass
class Class:
_name: str
@property
def name(self):
return self._name
@name.setter
def name(self, value: str) -> None:
self._name = value
Class(name="Name") # TypeError: Class.__init__() got an unexpected keyword argument 'name'
@dataclasses.dataclass
class Class:
name: str
_name: str = dataclasses.field(init=False)
@property
def name(self):
return self._name
@name.setter
def name(self, value: str) -> None:
self._name = value
c = Class(name="Name")
print(c.__dict__) # {'_name': 'Name'}
That's confusing. The SQLA docs use this as the example:
class Interval(Base):
# ...
@hybrid_property
def length(self) -> int:
return self.end - self.start
@length.inplace.setter
def _length_setter(self, value: int) -> None:
self.end = self.start + value
As always, the SQLA docs suck
I'm actually unfamiliar with MappedAsDataclass, but I'll look into it
Oh, I need that .inplace. Of course, instead of just saying what to do and why, the SQLA docs do a 3 paragraph explaination of of stuff, with a little bit at the end of how to get it working (with middling clarity)
Is this not how I add an item to a many to many with SQLA?
user.roles.append(admin_role)
db_session.add(user)
await db_session.commit()
I get sqlalchemy.exc.MissingGreenlet: greenlet_spawn has not been called; can't call await_only() here. Was IO attempted in an unexpected place? for the first line, and I cannot figure out why (I saw https://docs.sqlalchemy.org/en/20/errors.html#error-xd2s as the error pointed out, but I don't get it?
This is probably a mistake
I added the inplace and the error went away (to be replaced by the above error).
So the error is somehow related to lazy loading, but I cannot tell how, given I created the objects at that time:
user = await register_user(
email="me@email.com", username="username", password="test1234"
)
db_session.add(user)
await db_session.commit()
admin_role = Role(name="Admin", owner_id=user.id)
db_session.add(admin_role)
user.roles.append(admin_role)
db_session.add(user)
await db_session.commit()
This isn't problem with async, our "roles" relation wasn't initialized, so sqlalchemy tries to fetch it from db, it's not possible with async since you're just accessing the property (e.g. user.roles)
Perhaps just init it to initial empy array or add default to your mapped_column
Also don't commit multiple times during a single operation, or you may only partially commit your data
Can you expand on that? I don't follow. Specially since I need the id, which I won't have until it gets into the db, no?
any spark guides that are not unecessarily confusing and time wasting?
what percentage of people use postgres here? 90%? any love for oracle? 😆
oracle is very good if you need it, but it's very expensive
what are the different forms of authentication like making an app and i want to allow people to sign in,i know firebase is prevelant but has to pay is there any way i can do it myself for free?
As my table in my duckdb file approaches 40gb, the writes are starting to take a while, is there anything that's similar to duckdb files but more scalable
https://duckdb.org/docs/guides/performance/file_formats.html#parquet-file-sizes
Recommended file size is up to 10gb, can't you split your data based on something (e.g. date?)
Handling Parquet Files DuckDB has advanced support for Parquet files, which includes directly querying Parquet files. When deciding on whether to query these files directly or to first load them to the database, you need to consider several factors. Reasons for Querying Parquet Files Availability of basic statistics: Parquet files use a columnar...
I didn't know that, looks like I'll have to split it if I'm to continue using duckdb
I write most of my data to parquet, then ingest or query directly (agreeing with doctor)
I'm curious what are you using duckdb for? @fervent charm
Derivatives data
Historical or point in time?
Historical, minute resolution
I guess dumb question. Wouldn't be 40gb point in time
I honestly don't have much experience with timeseries data, but I heard that something like timescaledb could be good, but it depends on how you want to use your data afterwards
"Point in time" to me means unrevised. Like at that point in time it was thought that EPS for Q3 for a company was 3.4 but now we know it was really 3.2.
Minute resolution data can get problematic to work with.
Timescaledb is leagues slower than duckdb. I actually made a video on yt benchmarking this exact thing lol
I meant more; current value
How much slower is it and on what data/queries? 
Ho much data were you inserting?
Get it at the minute level and you don't need to store any other resolutions, just aggregate it up
5 million rows, 0.6 gbs
Yah, that's what I am getting at. If you can aggregate over a dimension first, then you can also partition along that dimension
start = time.time()
df.head(0).to_sql('data_table', pg_engine, if_exists='replace', index=False)
Are you sure this should be in the benchmark?
Well the minute level only is still takes up loads
Depends what function it came from, timescale had 3 methods, 2 were snail pace
timescale_benchmark_psy_csv
you seem to be loading data from dataframe, don't you think that pandas could be slow instead of timescale itself?
Where does df come from in duckdb benchmark?
duck_conn.execute("CREATE TABLE IF NOT EXISTS data_table AS SELECT * FROM df")
Yah, would love to see code from those comparisons. Every SWE loves to debate benchmarks
Yeah that's definitely possible, maybe I should change the name of the vid to "Benchmarking table-to-datafame operations"
Honestly to me the benchmark doesn't seem quite correct, you're utilizing duckdb internals to read and load the file into sql, while doing the same with pandas to load data into timescaledb?
of course performance could differ wildly here...
Comes from the function parameter
To_sql isn't the fastest way to load to Postgres
But it's in the query string? How does that work? 😅
Yeah, I know. Duckdb magic
(At least, executemany, but bulk loading is faster)
Yeah, the only postgres method out of the three that was able to do the write with any kind of speed used a lot of cur and conn stuff to load the df into the db
Can i get a dataset sample or the code to generate it?
Would like to test with sqlalchemy/postgres
And, if you're loading to DuckDB, then should be create table xyz as select * from 'myfile.parquet
Yeah sure, give me a second
To_sql is terribly inefficient. Only useful for small stuff, executemany is better, but there's faster
Honestly I don't even know how to read parquet files 😅
pd.read_parquet(aapl_filepath)
I usually read directly into DuckDB. Going through pandas just slows everything down.
Yeah you were telling me this and helping me do this in the Summer earlier this year
I've heard polars is faster?
polars is cool 🐻❄️
Anyway...
Polars uses pyarrows parquet reader, and is fast. DuckDB and polars are probably similar never compared directly here.
I am trying to work with polars in rust as we speak lol. The docs are not good.
You can load a pyarrow table from parquet, which has the added benefit of zero copy when used in DuckDB or polars (or pandas, if you set the backend/engine to pyarrow)
By the way I think what you linked to is talking about maximum size for parquet file size that duckdb can read from, not the .db files that duckdb stores data in
Yah, that's saying: each parquet file should be moderately sized. Above that, hive partition it
Right, my concern was with the actual .db size potentially getting too big. Or it might be the size of the table that's the problem tbh
They don't give you a good way to measure table size
Who doesn't? You can list table sizes/etc. https://duckdb.org/docs/sql/meta/duckdb_table_functions.html
DuckDB offers a collection of table functions that provide metadata about the current database. These functions reside in the main schema and their names are prefixed with duckdb_. The resultset returned by a duckdb_ table function may be used just like an ordinary table or view. For example, you can use a duckdb_ function call in the FROM claus...
I mean size in bytes, closest thing I see is "estimated_size The estimated number of rows in the table. "
How does postgres even work
Dont you have to like host a db somewhere and then connect to it
postgres does run as a separate server
That's right. You can even install Postgres as a docker container or a VM and then connect to it
thats pretty dumb tbh
how so?
ik its probably better like that but it just adds another layer of complexity
it has tradeoffs. it's easier to scale, but harder to setup
ig
You have to add up the blocks per table using storage_info, multiplied by block size, https://duckdb.org/docs/configuration/pragmas.html#storage-information
Above was for you. Ask in duckdb discord for more info
is text a database
What
any free real estate apis
also does the api give pictures or is that not what Apis usually do?
#python-discussion start here for general pythons discussion
Hi Guys I am working on a project API development using FastAPI frameworl and sqlalchemy as a orm tool. sqlalchemy returns a list of row objects, each object is key value pair of its attribute and entry when a query is returned to postman tool it gives response in JSON format. But for join operation sqlalchemy gives list of tuples, each tuple is a key-value pair. How to response this to postman in JSON format?
Hey anyone has idea about datacubes?
I want to create a datacube but I see very less resources
To learn
!rule 6 9 , <@&831776746206265384>
6. Do not post unapproved advertising.
9. Do not offer or ask for paid work of any kind.
This is not a job board, don't post this here please.
Yall give me lots of websites in this format:
URL
Name/Title
Description
Give me lots of them, its for a search engine that crawls a database
Lets go my websites DB hit 40GB, just took a year and 46 days
I'm using SQLite in a Docker container for a simple FastAPI app. Should I be trying to use async/await to access data in SQLite...or am I just asking for trouble
you can either use https://pypi.org/project/aiosqlite/ or just use the standard library sqlite3 module, it shouldn't make a difference unless you reach a point in which you shouldn't be using sqlite in first place
When you say it shouldn't make a difference. What do you mean? Obviously I introduce the possibility of more runtime errors with using async.
Talking specifically about sync vs async, the amount of time sqlite3 blocks operations for should be trivial
In theory the difference should become notable when you have a lot of concurrent requests trying to access the database at the same time, but if that becomes a problem, you should not be using SQLite in first place
I suspect that I'll never really hit that limit for reads for my needs. I use WAL mode in SQLite to optimize reads + writes.
The trick is that (as I understand it) I'm only using one process?
even a single process can deadlock itself if it involves multiple connections, although sqlite3 will at least timeout so your program isn't stuck forever: ```py
import sqlite3
conn1 = sqlite3.connect("test.db")
conn2 = sqlite3.connect("test.db")
conn1.execute("CREATE TABLE foo (x)")
conn1.execute("INSERT INTO foo VALUES (1)")
sqlite3 implicitly started a write transaction without committing.
If you unintentionally start another transaction at the same time:
conn2.execute("INSERT INTO foo VALUES (2)")
sqlite3 blocks for 5 seconds before timing out:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
sqlite3.OperationalError: database is locked``` if you're sure you always have no more than one pending transaction on a database, like by using an in-memory db or creating a connection with PRAGMA locking_mode = EXCLUSIVE, then you won't have to contend with locking, but of course, completely preventing concurrent transactions might not be practical for your app
besides that, you can try to be really cautious about how you manage transactions with sqlite3 (for example, never allowing context switches to occur while a transaction is open, and setting timeout=0 to raise "database is locked" exceptions immediately), or you can offload the I/O to a worker thread and let it wait for any locks to be released, which is what aiosqlite does
This is helpful thanks.
I think I might stick with non async implementation since just trying to implement it has introduced a lot of random exceptions and a bunch of time spent troubleshooting. For apparently not much benefit?
hi
import tweepy
from tweepy import OAuthHandler
import pandas as pd
def fetch_tweets(query, count=100):
# API credentials
# Authenticate
auth = tweepy.OAuthHandler(api_key, api_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
# Fetch tweets
# lang="en" tells the api to only fetch tweets in english, tweet_mode="extended" tells the api to fetch the full text of the tweets
tweets = tweepy.Cursor(api.search_tweets, q=query, lang="en", tweet_mode="extended").items(count)
data = [{"id": tweet.id, "text": tweet.full_text, "created_at": tweet.created_at} for tweet in tweets]
return pd.DataFrame(data)
if __name__ == "__main__":
query = "Python programming"
tweets_df = fetch_tweets(query, 100)
tweets_df.to_csv("tweets.csv", index=False)
i have this code to extract data from twitter
i am getting this error
File "c:\Users\McApple\Desktop\Python Projects\twitter_api.py", line 26, in <module>
tweets_df = fetch_tweets(query, 100)
File "c:\Users\McApple\Desktop\Python Projects\twitter_api.py", line 20, in fetch_tweets
tweets = tweepy.Cursor(api.search_tweets, q=query, lang="en", tweet_mode="extended").items(count)
^^^^^^^^^^^^^^^^^
AttributeError: 'API' object has no attribute 'search_tweets'. Did you mean: 'search_users'?
can some1 ping me or dm me. Thank You
hello?
The issue is in the error.
Search_tweets is not a valid method
Check what methods your api has
Or try the proposed
between postgre and mysql which one have better compatiblily with python?
both?
can I share my simple game that uses sqlite3? I'm learning databases and I'm just using gpt at the moment but I'm onto something I think
(learning concepts like ACID and crud, and concurrency but it's too advanced for me)
https://codeshare.io/R7e1rL (here) using a code share because its 178 lines
yo how do you hack the nasa?
Hello! I had a question! Is better if I use firebase or PostgreSQL with python and docker?
@app.route('/api/products/updateSize/<int:id>', methods=['PUT'])
def update_size(id):
product = Product.query.get(id) # دریافت محصول با شناسه
data = request.get_json()
edit_size = data.get("editSize", {})
for size, stock in edit_size.items():
product.size_availability[size] = int(stock)
db.session.commit()
return jsonify({'message':'successful', 'status':'ok'}), 200
```
```py
class Product(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), nullable=False)
type = db.Column(db.String(100), nullable=False)
size = db.Column(db.String(200), nullable=False)
size_availability = db.Column(db.JSON, nullable=True)
stock = db.Column(db.Integer, nullable=True, default=0)
color = db.Column(db.String(50), nullable=False)
gender = db.Column(db.String(50), nullable=False)
image = db.Column(db.String(100), nullable=False)
price = db.Column(db.Float, nullable=False)
def __init__(self, name, type, size, size_availability, stock, color, gender, image, price):
self.name = name
self.type = type
self.size = size
self.size_availability = size_availability
self.stock = stock
self.color = color
self.gender = gender
self.image = image
self.price = price
hello is there anyone to help why when i send request to update_size it doesent change the value in database from frontend sending json data is ok and it sending it with 0 problems. if anyone helps please reply or dm me
I didn't look closely but: You have a for loop, but return inside it... so it'd only complete one iteration. .
Can anyone assist me?
I am trying to create a program in python using Sqlalchemy, and also trying to dockerize it. However, I am also told that you should use migrations when using databases. So because of that, I am using alembic.
Using all three components together is very tricky though!
The issue is that I cannot generate the alembic migrations to begin with.
I feel as though when the docker composer is brought up it should run all available migrations and then start the main program. In order to create the migrations I have to do
alembic revision --autogenerate -m "Created state table"
but I feel like this should run on the HOST and not on the docker container itself.
When I attempt to run it on the host, it fails, because the container the mysql database runs on uses a docker container name, which my host doesn't know about.
I have tried asking chatGPT but no matter how I phrase it or what solution it gives me, it violates the basic parameters of this.
anyway, what I got:
Dockerfile:
FROM python:3.9-slim
WORKDIR /server
COPY server/ /server/
RUN pip install --no-cache-dir sqlalchemy pymysql alembic
COPY alembic /server/alembic/
COPY alembic.ini /server/alembic.ini
CMD ["sh", "-c", "alembic upgrade head && python -u main.py"]
docker-compose.yml:
services:
db:
image: mariadb:10.5
container_name: db_test
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: game
MYSQL_USER: user
MYSQL_PASSWORD: password
ports:
- "5101:5101"
volumes:
- mariadb_data:/var/lib/mysql
server:
build: .
container_name: game_test
ports:
- "5100:5100"
depends_on:
- db
#restart: on-failure
migrations:
build:
context: .
container_name: alembic_migration
depends_on:
- db
environment:
SQLALCHEMY_URL: mysql+pymysql://user:password@db_test/game
command: alembic upgrade head
volumes:
mariadb_data:
server/main.py:
import time
from sqlalchemy import create_engine, Column, String, Integer, MetaData, Table
from sqlalchemy.orm import declarative_base, sessionmaker
Base = declarative_base()
metadata = MetaData()
class Foo(Base):
__tablename__ = "foo"
id = Column(Integer, primary_key=True, autoincrement=True)
bar = Column(String(50), nullable=False)
fruit = Column(String(10), default="banana")
veggie = Column(String(10), default="broccoli")
DB_USER = "user"
DB_PASSWORD = "password"
DB_HOST = "db_test"
DB_NAME = "game"
DATABASE_URL = f"mysql+pymysql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}/{DB_NAME}"
print(DATABASE_URL)
time.sleep(3)
engine = create_engine(DATABASE_URL, echo=True, connect_args={"charset": "utf8mb4"})
Base.metadata.create_all(engine)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
with SessionLocal() as session:
print(session)
new_row = Foo(bar="baz")
session.add(new_row)
session.commit()
print("table created")
alembic.ini
alembic]
script_location = alembic
prepend_sys_path = .
version_path_separator = os # Use os.pathsep. Default configuration used for new projects.
sqlalchemy.url = ${SQLALCHEMY_URL}
[post_write_hooks]
[loggers]
keys = root,sqlalchemy,alembic
[handlers]
keys = console
[formatters]
keys = generic
[logger_root]
level = WARN
handlers = console
qualname =
[logger_sqlalchemy]
level = WARN
handlers =
qualname = sqlalchemy.engine
[logger_alembic]
level = INFO
handlers =
qualname = alembic
[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic
[formatter_generic]
format = %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %H:%M:%S
directory listing:
(.venv) [sean@arch dockertest3]$ (master)tree
.
├── alembic
│ ├── env.py
│ ├── __pycache__
│ │ └── env.cpython-313.pyc
│ ├── README
│ ├── script.py.mako
│ └── versions
├── alembic.ini
├── commands
├── docker-compose.yml
├── Dockerfile
├── requirements.txt
└── server
├── main.py
└── __pycache__
└── main.cpython-313.pyc
Please let me know if I'm going about this completely the wrong way. I am quite lost and frankly a little frustrated.
Should I just not create migrations at all?
Need help with mysqldump on Mac Terminal
note, I haven't worked with the Mac or used this particular tool, but it always helps to describe your full problem (you can save and repost the text as needed either here or in other venues).
I am migrating to a new laptop, and I want to backup my current website and database (MySQL). I have an old script that worked fine before in Mac Terminal, but I am getting an error. (Have been "Spring cleaning" and changed the filings tructure, but that shouldn't matter.)
I get this error in Terminal...
zsh: command not found: mysqldump
Here is my command string....
/Applications/MAMP/Library/bin/mysqldump --skip-extended-insert --host=localhost -uroot -p db_dev > '/Users/user1/Documents/4 - INFORMATION TECHNOLOGY/4 - DEVELOPMENT/DB_BACKUPS/db_dev_2024-12-31_0927.sql'
I don't think you're not supposed to generate your alembic migrations in docker, unless you're developing in a container, which I don't have any experience with.
Personally I just run alembic on my machine when developing
is there mysqldump in /Applications/MAMP/Library/bin?
Yes
try changing into that directory and running it without the path
e.g. mysqldump --skip-extended-insert --host=localhost -uroot -p db_dev > '/Users/user1/Documents/4 - INFORMATION TECHNOLOGY/4 - DEVELOPMENT/DB_BACKUPS/db_dev_2024-12-31_0927.sql'
I think so, what happens when you do that?
I see bin %
and ls gives you what?
zsh: permission denied:
ok, there may be a permissions issue going on here. I don't have enough Mac experience to know really
A whole bunch of programs including mysqldump
The only thing that changed is where I store files
Could spaces cause issues?
you have the path quoted, but I don't know if that's how zsh handles paths with spaces in them
Like... '/Users/user1/Documents/4 - INFORMATION TECHNOLOGY/4 - DEVELOPMENT/DB_BACKUPS/db_dev_2024-12-31_0927.sql'
Let me try something...
Just tried...
/Applications/MAMP/Library/bin/mysqldump --skip-extended-insert --host=localhost -uroot -p db_dev > '/Users/user1/Documents/db_dev_2024-12-31_0927.sql'
Am looking at this now...
(I am totally not a command-line kind of person)
Because this is a utility for MySQL, I'm leery to start changing file permissions
yeah, I'm not a Mac guy, but I will say that learning more about the command line on whatever platform you use is vital for this kind of work
Could it be a corrupt OS? (One reason I am trying to get everything of fthis old MBP before year's end (~ < 12 hrs) is because the logic booard is toast
But other than rearranging my folders, nothing else has changed, and I just copied and pasted the old command - changing paths - which worked a couple years ago
I don't have enough Mac experience to help with that, sorry
Are you proficient with Linux or even Windows command line?
yes, but the Mac is very much its own animal.
maybe not
I had this really strange issue a couple years ago where I couldn't get MAMP working. And after like a week of researching, I had to do this...
(Lost my command history)
su user2
can't find other commands, but yoyo mysql
The point being maybe I am logegd in as Admin (user2) now and that is causing issues?
I don't know. I don't know enough about the Mac to say.
How would you un sudo yourself back into the current account?
I don't know.
The only thing I can think of would be to close the current terminal session
ok, so it was permissions!
More so that I was still logegd in as user2 (admin)
Crap, now I last my saved commands
At least mysqldump is working
Must be your presence that inspired me! 😉
Glad I was of some help, even if I have no Mac experience as such
Sometimes thinking out loud talking with someone else about unrelated things triggers the brain
I just want to get my life off this old MBP and onto my new MBP so I can get to Python soon!
Rubber Duck Debugging
What's that?
exactly what you described! :D
In software engineering, rubber duck debugging (or rubberducking) is a method of debugging code by articulating a problem in spoken or written natural language. The name is a reference to a story in the book The Pragmatic Programmer in which a programmer would carry around a rubber duck and debug their code by forcing themselves to explain it, l...
52
Cool
I'll be 54 soon
Thanks for the term and link
Now that my computer is Feng Shui'ed, I can file that away and actually refer to it
As I was doing that, I stumbled across some old stuff which took me online which lead me to find out that some really knowledgeable people I knew online had died
note that if we're done with this we should let the channel go back to topic, we try to keep the channel discussions focused on the topic at hand
Itw as a start reminder of how little time I have left
Okay, was just saying that I appreciate "older" people who know about terms like you posted
When all of these people die off, so much knowledge is going to get lost
(I'll be sure to file away as much as I can while people like you are around!)
Thanks!
BTW...
This is what caused the earlier issue
su user2
sudo /Applications/MAMP/bin/stopApache.sh
sudo /Applications/MAMP/bin/startApache.sh
(I need to do that - annoyingly - everytime I launch MAMP so it works because there is something broken on this old Mac...)
Is there any reason why a local postgres instance might be taking exactly 60 seconds to connect?
the database tool in my IDE (IntelliJ) connects instantly without any issues.
I have the service defined in a docker-compose:
postgres:
image: postgres:15-alpine
container_name: database
environment:
POSTGRES_USER: DEVELOPMENT
POSTGRES_PASSWORD: DEVELOPMENT
POSTGRES_DB: facadectl
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
I think IntelliJ uses the jdbc driver
I should also mention that it eventually succeeds, but it just takes outrageously long to connect
How are you trying to connect to it?
It experiences that connection delay with psycopg2 and pg_connect
But what exactly are you doing? It's very difficult to tell what's wrong without any code
Ah, it's psql, not pg_connect.
psql -U DEVELOPMENT -h 127.0.0.1 -p 5432 facadectl
Same issue though. It takes 60 seconds to connect. I've provided basically all of the relevant code.
Huh, that's weird... I'm not at PC right now, but can try to reproduce it when I can
It's possible it might be a docker x macOS issue
I've just resorted to sqlite for now
Maybe
, I never had such issue tbh
Hi. I need some help with MySQL on my Mac. In the process of migrating everything over from my old MBP to a new MBP, and it appears that I broke MySQL and/or phpMyAdmin 😭
Command-line is not my thing, and so I'm flailing right now. (Damn! And I wanted to have my migration done by 2025!)
whats the problem exactly?
Where to begin? Okay so I am migrating from this old MBP to a new MBP. (Wanted to haev it done by 2025, but that isn't looking likely.)
I use MAMP for my webserver and MySQL server and phpMyAdmin to adminster things.
m apache m php something?
I am here because I want to learn Python and replace this old site with a new one, but first i need to get my old dev environment and website working on my new Mac
Mac Apache MySQL PHP
okkay :>
So after I installed MAMP on my new MBP, I spent a lot of time tweaking config files to supposedly make MAMP work. (I put in a bunch of dummy placeholder files in various directories so I know here to update stuff. Have been using MAMP for like 15 yrs)
Updating things like the php.ini is a bitch, but I am experienced with things
Oh, I was able to get my old website working on this old MBP using MAMP so I know my code and db work
So anyways - sorry but this is complicated
I was ready to launch my website on my new MBP and I got some errors in FireFox.
It said the table that I wrte errors to was missing - because I had some PHP errors to because of the newer install
So, when I go into phpMyAdmin, I see all of my tables
BUT when I clicked on the log_view_page table phpMyAdmin says... #1146 Table 'db_dev.log_view_page' doesn't exist
Here is what I am wondering...
(Sorry, I am going back and forth between two laptops in the front seat of my car in the dark!)
I just copied the directory that supposedly holds my database from old to new laptop
you should probably take a dump!
mysqldump -u [username] -p [database_name] [other_database_name] -R -e --triggers --single-transaction > database_backup.sql
I see all of these files - with filesizes larger than 0 - on my new laptop, so the database should be there, but maybe you can't just copy and paste a database like that?
You have to understand that life came up and I havent done any of this in 10 years...
I just want to get my old dev environment and website working so I can quickly pivot to coding a new website
i don't know. Maybe it works, maybe not 😄 But taking a dump is the way a backup is meant to be taken
Do you think copying and pasting is the issue?
I have that command saved into a file for reference, but I didn't use in in my new install of MAMP because I thought it maybe only exported the data and not the database structure?
This is what I had in notes...
/Applications/MAMP/Library/bin/mysqldump --skip-extended-insert --host=localhost -uroot -p db_dev > '/Users/user1/Documents/some_path/db_dev_2024-12-31.sql'
Dont recall if that was just data only of the DDL or whatever as well
If I move the copied directory to the side, will it interfere with trying mysqldump?
I Googled the error and one site said it was just a permissions issue
I mean the database directory I copied from my old MBP, can I just move to to another directory, try mysqldump and wanted to be sure it would screw up the original database files I can see
You could be right, because when I tried the solution it didnt work
(I can''t have two laptops in one lap)
One site suggested running SHOW TABLES
But I cant figure out how to get the MySQL prompt in Mac Terminal for MAMP
write mysql?
Could you help me figure that out? (Because first I need to prove if the tables exist or not in mysql)
Before doing that, I tried this..
/Applications/MAMP/Library/bin/mysql -uroot -p
And get Enter password
But then I get
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/Applications/MAMP/tmp/mysql/mysql.sock' (2)
Your suggestion yields
zsh: command not found: mysql
mysql -h
nm 😄 You explained it cannot connect
ps axu| grep mysql
is the server running?
also, you can give it hostname and port to connect via tcp
Another site said to yo yo MySQL. Maybe I broke it?
mysql -u root -p -h 127.0.0.1 -P 3306
check the switches
that they're correct
with mysql --help
user1 1628 0.0 0.0 34122828 836 s000 S+ 9:36PM 0:00.00 grep mysql
that's the only row?
yes
sure
user1 1763 0.0 1.1 34542240 180264 ?? S 9:37PM 0:00.41 /Applications/MAMP/Library/bin/mysqld --basedir=/Applications/MAMP/Library --datadir=/Applications/MAMP/db/mysql57 --plugin-dir=/Applications/MAMP/Library/lib/plugin --log-error=/Applications/MAMP/logs/mysql_error.log --pid-file=/Applications/MAMP/tmp/mysql/mysql.pid --socket=/Applications/MAMP/tmp/mysql/mysql.sock --port=3306
user1 1649 0.0 0.0 34134512 1420 ?? S 9:37PM 0:00.03 /bin/sh /Applications/MAMP/Library/bin/mysqld_safe --port=3306 --socket=/Applications/MAMP/tmp/mysql/mysql.sock --pid-file=/Applications/MAMP/tmp/mysql/mysql.pid --log-error=/Applications/MAMP/logs/mysql_error.log
then try with mysql -u root -p
When I launched MAMP, and I go into NetBeans to run my website, it should work because I got it working yesterday on my old laptop
But when I launch it in NetBeans, I get this in Firefox
Forbidden
You don't have permission to access this resource.Server unable to read htaccess file, denying access to be safe
WTF?
Now I have two broken laptops?
.htaccess is a apache thing
(I don't have Discord set up on my new laptop yet)
did you tinker apache to run under different user or did you change the files it serves any way? The running web-server process may not have permissions to read the .htaccess (is my guess)
Disregard last comment. My old laptop and MAMP are screwed up and to get things to work I have to su user and then yo-yo Apache
So my website IS working again on this old laptop
But I cannot seem to get phpMyAdmin to work
And I was trying your commands here, because the end goal is to see if I have tables in MAMP's MySQL
that.... i don't know. I don't really know apache nor phpmyadmin
So what works here should work on my new laptop, right?
yes, if the database is ok, and you dump everything it should work
And since I know my website works here, then if I can figure out how to SHOW TABLES in Terminal, then I can use that on my new laptop to verify if they are there
No, but I need to learn how to use it with 10,000 other things!
no. I was hoping it might make your life easier, but it's not a requirement for anything
I'm nervous about mysqldump because I dont think it's so easy to copy over all the db objects as one might think
it's purpose built for that.
How would Homebrew help here?
mmm... it's a package manager. It's easy to install database, web server and python with it
After 10 years off, I don't remember what it does and doesnt do, but I know my db has stored procedures and trigegrs and I'm afraid of breaking any of this since I feel like I have Windows 95 and no tech support!
you need to dump those also
check the switches, and make sure you select everything you need to dump
mysqldump --help
If I opened a backup I already did, would it help me determine if it backs up the db objects too?
you can just open the .sql file and check what it inserts, that it defines triggers and procedures and views and whatnot
after dumping
and if you missed some, you can do again
and if you notice after loading the dump onto the new macbookpro that you missed something, you can still go back and do a dump
So you know MySQL?
a little. We had a course using mariadb at school, but i use postgres more
What keyword would be used for Triggers and Stored procedures?
i don't remember. Lemme check ;D
you can search for create procedure for procedures
I see this...
replace procedure with create trigger
/!50003 CREATE/ /!50017 DEFINER=root@localhost/ /!50003 TRIGGER checkAddArticle BEFORE INSERT ON article_placement FOR EACH ROW BEGIN
SELECT COUNT() INTO @sdCount
FROM section_dimension
WHERE section_slug = NEW.sd_section_slug
AND dimension_slug = NEW.ds_dimension_slug;
IF @sdCount = 0 Then
INSERT INTO invalid_section_dimension_pair
VALUES(null);
END IF;
SELECT COUNT(*) INTO @dsCount
FROM dimension_subsection
WHERE dimension_slug = NEW.ds_dimension_slug
AND subsection_slug = NEW.ds_subsection_slug;
IF @dsCount = 0 Then
INSERT INTO invalid_dimension_subsection_pair
VALUES(null);
END IF;
END */;;
DELIMITER ;
That is my code for sure, but it almost looks like it is commented out?
did you use switch --triggers
2025
btw, you can paste code with triple-backticks and have syntax hilighting by using the lang after. ```sql select 1 ```
Back, just dictated a quick note to myself on my iPhone for New years
No
then the dump won't include triggers
But is that not my trigger above?
/Applications/MAMP/Library/bin/mysqldump --skip-extended-insert --host=localhost -uroot -p db_dev > '/Users/user1/Documents/db_dev_2021-02-18_2033.sql'
That is from 4 years ago
i don't know why it works like it does. But if it is commented out it probably is ignored when the dump is loaded
you want everything, right?
Everything
--routines for routines, --triggers for triggers ... Maybe something else too? Or not? You probably know. Also double check those, i dont have mysql installed, so i check the switches from webs
Anyone here that might know phpMyAdmin?
Sounds like you think my copy & paste is the issue even though I have all of the db files, right?
i think it might be. I don't know. It's merely a guess
but i do know that dumping is made for backing stuff up
I'm just scared of mysql dump because I dont recall what all my website and database did. And the goal is to just get it migrated so I have it as a working reference when I build my new site using Python
I guess I can Google the topic more tomorrow and maybe someone has written an article or YT video on how to back up everything
sure
I thought the backups I did in the past worked, but I dont remember
I gotta head home
Thanks for trying to help, and I hope I dont break things
i hope you get it working, and good luck :>
It's always one last details that breaks things!
I'll be back tomorrow. Thanks for the help and Happy 2025!
Happy new year to you aswell!
I'm interested in making my own database framework what makes a database framework like NoSQL scalable
Need help navigation Mac Terminal and trying to see if my database tables are there after a migration
I was able to figur eout the corrcet path to run mysql in Terminal. Now I could use some advice from someone who is experienced using mysqldump
I fixed it without needing mysqldump. Turns out there were like four files starting with ib_ in Applications > MAMP > db > mysql57 that I didn't bother to copy over, but which phpMyAdmin needed. Web legacy website is now working on my new laptop!! YEAH!!!
rewriting my old sql3 wrapper and i rll like how its turning out rn, any tips?
might make it open-source
it does recognise tables and all that for you as well as primary keys so something like
with data.open() as db:
db["yourTable"].findFirst(primaryKey)
would work just fine
also gonna add db.pause and db.resume to commit and close the db for a larger computations without making a new db object
confused on how to link a db to a discord bot that my friend allowed me to use; running off replit and a vps
why r u using replit and a vps?...
search up documentation, i dont use postgres
fuck
are you using a standalone postgresql or are you actually using Supabase that's powered by Postgresql?
Who
Sqlite3 wrapper for Python. Contribute to kvandeh/sqlite3wrapper development by creating an account on GitHub.
Yall, do not try to do parallel writes to duckdb files, with time.sleep and stuff. It'll end with a corrupted db
what should i name a db column having number of pages in a book? num_pages, pages, page_count... anything else? its in a table called books.
I like page_count
When I tried to backup my database from a local postgresql container for my django project, I wanted to test the backup sql script by putting it up on Supabase. I found that the pg_dump uses stdin and then inserts each row into the table as opposed to using INSERT.
I used docker-compose exec -T postgres pg_dump -U postgres -d mydjangosite > backup.sql
How do I change this so that I can have a working backup sql script?
add --inserts for INSERT INTO on each line I think
I mean, rather than COPY @cedar tiger
I'll try that thanks
That worked. Thanks!
hi guys, I want to extract this table to a personel spreadsheet. How can I do that?
if a client has a post request that gives the entire blocked and friends lists, would it be ok to store this in my database on the user table as a json array, or should i make a seprate table for friends and blocked users
Try pandas.read_html
(Assuming it's on some webpage)
It depends. My guess is that you may want to make relational queries against that data, which means you should probably have tables for it. But if not, then maybe not.
Anyone tried switching from psycopg2 to asyncpg, and then getting "another command is already in progress" errors, which did not happen with psycopg2?
The cause of this issue is that you're trying to send multiple queries at the same time on one connection, which isn't allowed by postgres
@paper flower yes, but the thing is, it has always worked perfectly with psycopg2, but after switching to asyncpg, the issues show up.
This issue appears during tests (using pytest) , where around 5% will now fail.
Just seems strange that psycopg2 had no issues with this 🤔
Your code with psycopg2 probably was syncrhronous and never was running concurrent queries unless you used threads
how would you lot check if a folder is changed?
would you check everytime the program is loaded? or would you do it everytime its loaded at a certain time with an option to reload?
its a huge folder, im looking for specific filetypes, and im looking in 4 specific folders
got it, store len(gdt_files)
then scandir, len([entry for entry in os.scandir
if len == stored len
do you need to know when a file is modified?
entirely a folder really
read all files and digest them into md5 hash
if md5 hash changed => then folder changed content
there is some kind of a feature where u can "listen" to events of filesystem and react accordingly
if wishing super optimization
It's just slow to convert file structure to JSON is all
os.walk laggy
then use smth more optimal than json
actually, you are reinventing the wheel, i know python library that already solved it, a moment
I need to walk 4 folders look for *.gdt and dump their filepaths to
gdt[mainpath]
pip install watchdog[watchmedo]
watchmedo auto-restart --patterns "*.templ;*.go;*.css;*.js;enverant.json" -i "*_templ.go" --recursive -- sh -c "task web; sleep infinity"
i can specify which file patterns to listen, and any arbitary command to run on file changes
^_^
my favourite thing for dev env
task web; sleep infinity is the arbitary command running on file changes
Ok, gotta find a model viewer now
Might write my own
Turns verts and indices into a mesh
I'm just getting started using sqlalchemy for the first time (I'm very familiar with Django ORM). I have some dataclasses that I'd like to use "as is" with sqlalchemy, but the docs seem to imply that I should use MappedAsDataclass, which to me means that I should define them as sqlalchemy objects instead of as dataclasses. Is that the correct way to go about this? I was hoping there was just an easy wrapper I could put ontop of a dataclass lol 🤷♂️
anyone out here who could help me in a small problem im facing????
basically this a school assignment of mine and apparently i had to rewrite a to-do list program with mysql connectivity integration.
i need to know how can i make the sql command so that whenever a task is entered , it auto numbers them , aka automatically assigns the primary key id numbers
the table structure is (id int primary key , task varchar(50))
def add_task():
task = input("Enter the task: ")
b = connect()
c = b.cursor()
c.execute("INSERT INTO tasks (task) VALUES (%s)", (task,))
b.commit()
print(f'Task "{task}" added!')
b.close()
part of the code im struggling with
like when i enter a task say do chores , it should execute
c.execute("INSERT INTO tasks (task) VALUES (1, 'do chores')")
and when i input another tasks , do homework , it should automatically assign it the next number for the id
like c.execute("INSERT INTO tasks (task) VALUES (2, 'do homework')")
do ping if any of yall help
I'm on mobile so it's hard to type up an explanation but check this out: https://dev.mysql.com/doc/refman/8.4/en/example-auto-increment.html
ALTER TABLE tasks MODIFY id INT AUTO_INCREMENT;
This will modify your table such that id column will automatically increment
one small issue , while this does works fine , but my program also has a delete tasks function which removes the selected task (row) from the table , and when i do so , the next task entered has the next sequential number assigned to id
say task1 was entered , it got assigned id = 1
i deleted task1 , no more tuples in the table
i add a new task 'task2' , but this time the id=2 when it should have been 1 cuz this is the first and only tuple present in the table
def remove_task():
view_tasks()
try:
task_id = int(input("Enter the task number to remove: "))
b = connect()
c = b.cursor()
c.execute("DELETE FROM tasklist WHERE id = %s", (task_id,))
if c.rowcount > 0:
print(f'Task with ID {task_id} removed!')
else:
print("Invalid task number.")
b.commit()
b.close()
except ValueError:
print("Please enter a valid number.")
this is the remove task function in my program
Which database is best for linux operation system with alot of requests
Postgresql can be a default choice
fitting 95%+ usage cases
majority of people have no chances to reach its limits
Ok thx
what if I have 2 tasks, t1 and t2. the ids are 1 and 2. I then delete t1 then insert t3. what should t3's id be
2 , since after deleting t1 the order will be something like this
1, t2
2, t3
so all the task ids will "slide up"?
Yes
i see. a row's primary key should not change, so instead we will just use a different mechanism for numbering the tasks and keep the id column as it is
do you need the numbering to be done in the database itself?
!e If you let the database do it, t3 will have id 3.
import sqlite3
conn = sqlite3.connect(":memory:")
conn.execute("create table tasks(id integer primary key, name text)")
conn.execute("insert into tasks(name) values (?), (?)", ("t1", "t2"))
print(conn.execute("select * from tasks").fetchall())
conn.execute("delete from tasks where name = ?", ("t1",))
conn.execute("insert into tasks(name) values (?)", ("t3",))
print(conn.execute("select * from tasks").fetchall())
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | [(1, 't1'), (2, 't2')]
002 | [(2, 't2'), (3, 't3')]
Hello everyone!
For the past few months, I've been working on ORMagic - a simple, fast, and lightweight ORM for Python, built on top of Pydantic .
Today, I'm excited to announce a major update that adds PostgreSQL integration!
The main goal of ORMagic is to provide a simple and easy-to-use ORM for Python, that is easy to understand and use, while still providing the necessary features to interact with a database.
Is based on the Pydantic model and extends it with the ability to interact with SQLite and PostgreSQL databases.
Simple example
from ormagic import DBModel
class User(DBModel):
name: str
age: int
User.create_table()
User(name="John", age=30).save()
User.get(name="John")
>>> User(id=1, name='John', age=30)
You can find more examples and detailed documentation at spaceshaman.github.io/ORMagic/
If you like the project, please give it a star on GitHub: https://github.com/SpaceShaman/ORMagic
I also welcome any feedback or suggestions, so feel free to open an issue. The project is open to pull requests as well!
Thank you for your support!
Nice! But if I have to go through defining the database tables through ORM, I would just use Django for this. Is there a reason to use ORMagic vs Django's ORM? https://docs.djangoproject.com/en/5.1/topics/db/queries/
There is somewhat known (15k stars) ORM SQLmodel from fastapi creator
That uses Pydantic centric approach too
Could be good to know your project difference from it
https://github.com/fastapi/sqlmodel
What is your project brings unique new to the table in comparison and having it written in docs
Django is not typing friendly at all. 😄
Plugins to make it typing friendly are rather fragile/crude hacks on top
I don't think there's any benefit in adding pydantic to orm(s)
As good base as any. Django orm is typing not friendly
Sqlalchemy is poorly documented(and somewhat looking suffering from bloat) and having somewhat bothersome migrating system
Making new orm on Pydantic basis looks cool here if it will take care of migrations properly
I wouldn't agree that sqlalchemy is poorly documented though
It covers a lot of use cases, there certainly isn't a lack of documentation
Regarding typing I there's still isn't a reason to use pydantic, even it's __init__ is using mypy plugins to get proper typing 🤔 On the other hand it shouldn't be difficult to use dataclasses or dataclass_transform like sqlalchemy does
I know SQLModel and I don't really want to compete with it (for now I develop ORMagic alone in my free time, just for fun). The main difference between these two libraries is a different approach to the API which seems more intuitive to me, in ORMagic all interactions with the database take place using classes defining models (similar to Django but with better type annotation thanks to the use of Pydantic models), in addition I try to make my library have as little dependency as possible (in the case of the basic version without Postgres, it only depends on Pydantic)
I also plan to add migration support similar to what we can see in Django but with a few improvements, such as searching all files in the project for models and changes to them. But it will probably take me some time because, as I mentioned, I am implementing this project in my free time 😉
I don't think active record pattern is good. How would one go about using two databases at the same time or using sharding or similar techniques?
Sorry for the late reply , yes that's right , I just need the id to be 1 2 3 ...... despite whatever the changes are done to the tasks
Could you explain what are you doing? Why do you need a specific order? Should it allow for any gaps at some times or should all the records be shifted when a record from the middle is deleted?
How many records would you have?
Teachers when checking it wouldn't enter more than 5 records , imma send my whole code , you might get the idea what I'm trying to do
import mysql.connector as a
def connect():
return a.connect(host='localhost',
user='root',
password='upps',
database='todo_list')
def display_menu():
print("\nTo-Do List Menu:")
print("1. Add Task")
print("2. View Tasks")
print("3. Remove Task")
print("4. Exit")
def add_task():
task = input("Enter the task: ")
b = connect()
c = b.cursor()
c.execute("INSERT INTO tasklist (task)
VALUES (%s)", (task,))
b.commit()
print(f'Task "{task}" added!')
b.close()
def view_tasks():
b = connect()
c = b.cursor()
c.execute("SELECT id, task FROM tasklist")
tasks = c.fetchall()
if not tasks:
print("No tasks in the list.")
else:
print("\nYour Tasks:")
for task_id, task in tasks:
print(f"{task_id}. {task}")
b.close()
def remove_task():
view_tasks()
try:
task_id = int(input("Enter the task number to
remove: "))
b = connect()
c = b.cursor()
c.execute("DELETE FROM tasklist WHERE
id = %s", (task_id,))
if c.rowcount > 0:
print(f'Task with ID {task_id} removed!')
else:
print("Invalid task number.")
b.commit()
b.close()
except ValueError:
print("Please enter a valid number.")
def main():
tasks = []
while True:
display_menu()
choice = input("Choose an option (1-4): ")
if choice == '1':
add_task()
elif choice == '2':
view_tasks()
elif choice == '3':
remove_task()
elif choice == '4':
print("Exiting the To-Do List. Goodbye!")
break
else:
print("Invalid choice. Please try again.")
Could you explain what are you trying to do and why you want to remove gaps from the sequence?
how do i restore a db?
Uhh 😬
Magic
It helps to give more context around the situation.
Could some one point me in the direction of a good guide to explain Cartesian join or table joins . I’m having troubles figuring out how it if you join two table with different amounts of columns . How can a single table be created then with no empty spaces .
the number of columns isn't important, if you join a table (A, B) with (B, C, D), the result will just be (A, B, C, D)
!e I think that's join and database dependent, eg SQLite:
import sqlite3
conn = sqlite3.connect(":memory:")
conn.execute("create table table1(a,b)")
conn.execute("insert into table1 values('a','b')")
conn.execute("create table table2(b,c,d)")
conn.execute("insert into table2 values('b','c','d')")
# double B
print(conn.execute("select * from table1, table2").fetchall())
print(conn.execute("select * from table1 join table2").fetchall())
print(conn.execute("select * from table1 join table2 on table1.b = table2.b").fetchall())
print(conn.execute("select * from table1, table2 where table1.b = table2.b").fetchall())
# single B
print(conn.execute("select * from table1 join table2 using(b)").fetchall())
print(conn.execute("select * from table1 natural join table2").fetchall())
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | [('a', 'b', 'b', 'c', 'd')]
002 | [('a', 'b', 'b', 'c', 'd')]
003 | [('a', 'b', 'b', 'c', 'd')]
004 | [('a', 'b', 'b', 'c', 'd')]
005 | [('a', 'b', 'c', 'd')]
006 | [('a', 'b', 'c', 'd')]
was only talking theoretically
but yeah I should have clarified. if you were joining on B you get that result. in general you'd get B twice
I’m fairly new so I’m still trying to grasp the concepts . I’m using phpMyadmin for a course and how it was presented was :
How then is a new table created with no missing information . If table 1 has 3 columns and table 2 has 11 columns . Shouldn’t there be empty spaces where table one column does not have information for?
Sorry for the confusion
what do you mean by "missing information"? what do you expect to happen?
Azure Cosmos DB Conf 2025
April 15th 2025, virtual conference.
Call for Proposals is now open until Feb 25. https://aka.ms/CosmosConfCFP
Azure Cosmos DB Conf is a free virtual developer event co-organized by Microsoft and the Azure Cosmos DB community. This is our fifth Azure Cosmos DB Conf. Sessions will be a combination of Microsoft and community delivered. Come and learn more about Azure Cosmos DB. See what others in the community are building.
hm
try to Add a locked field to the coinflips schema to mark whether a coinflip is being modified
like
locked: { type: Boolean, default: false },```
when someone joins it sets active to false already
isnt that good?
Hey guys, good afternoon. I have the following situation:
I need to create a django API from a database made in postgresql 12.4. The thing is: the only version that supports postgresql 12.4 is django 4.1. What can i do?
Using django 4.1 and not the latest version can be a problem? in terms of features of django?
Exceptions:
I cant upgrade the database because is an active database (its on production in odoo 12 so it will be imposible unless i try to upgrade odoo which isnt going to happen)
Hey guys, one question, why when creatin geometry point like this in pgadmin
SET alberghi_location = ST_GeomFromText('POINT(' || Longitudine || ' ' || Latitudine || ')', 4326);```
I still get data like this 0101000020E61000005803EF3F0D6E3140C1C9FE2957584440
and not in this format "POINT(17.429889675 40.6901600355227)"
hi guys, quick question.
in models.py for sqlalchemy
lass KnownPlanets(Base):
__tablename__ = "known_planets"
user_id = Column(Integer, ForeignKey("users.id"), primary_key=True)
planet_id = Column(Integer, ForeignKey("planets.id"), primary_key=True)
__table_args__ = (
#TODO: a primary key for two columns doesn't work
#PrimaryKeyConstraint("user_id", "planet_id", name="pk_known_planets"),
PrimaryKeyConstraint("user_id", "planet_id"),
{"mysql_collate": "utf8mb4_unicode_ci"},
)
when I run "SHOW CREATE TABLES known_planets" in the sql command line:
known_planets | CREATE TABLE `known_planets` (
`user_id` int(11) NOT NULL,
`planet_id` int(11) NOT NULL,
KEY `user_id` (`user_id`),
KEY `planet_id` (`planet_id`),
CONSTRAINT `known_planets_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`),
CONSTRAINT `known_planets_ibfk_2` FOREIGN KEY (`planet_id`) REFERENCES `planets` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci |
So the two keys I had set explicitely to be primary keys are both marked as simply "KEY".
This makes it so if I insert an entry that has the same (user_id, planet_id) as an entry that already exists, it inserts it anyway.
chatgpt and various resources online tell me it should prevent it.
When creating the table from the command line, I can create composite keys just fine, and duplicate entries aren't being created so it's not an issue with the version of mariadb I'm running.
Goal: create a composite key with sqlalchemy
Issue: I can't, and duplicate entries are being created
What am I doing wrong?
That's weird, did you use alembic or Metadata.create_all to make that table?
alembic
Could you share the migration file?
uhh I am really, really bad with alembic (I had just fixed an issue i had for the past there weeks)
i can try?
this?
"""changed primary key of knownplanets
Revision ID: 6444b4b10c02
Revises: e137714a90f8
Create Date: 2025-01-18 00:16:34.075779
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql
# revision identifiers, used by Alembic.
revision: str = '6444b4b10c02'
down_revision: Union[str, None] = 'e137714a90f8'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.alter_column('known_planets', 'user_id',
existing_type=mysql.INTEGER(display_width=11),
nullable=False)
op.alter_column('known_planets', 'planet_id',
existing_type=mysql.INTEGER(display_width=11),
nullable=False)
op.drop_index('ix_known_planets_id', table_name='known_planets')
op.drop_column('known_planets', 'id')
# ### end Alembic commands ###
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('known_planets', sa.Column('id', mysql.INTEGER(display_width=11), autoincremue, nullable=False))
op.create_index('ix_known_planets_id', 'known_planets', ['id'], unique=False)
op.alter_column('known_planets', 'planet_id',
existing_type=mysql.INTEGER(display_width=11),
nullable=True)
op.alter_column('known_planets', 'user_id',
existing_type=mysql.INTEGER(display_width=11),
nullable=True)
# ### end Alembic commands ###
I'm confused why...oh
I think i know whats going on.
let me confirm
You had id column as primary key before?
yeah i got rid of it but i dont think thats it
alembic may not be the best with detecting these types of changes
I think you can make a pk yourself
(in a separate migration)
look at the date it was created.
today, but shortly after 12...AM, not PM.
Essentially I was neglecting to actually create the migrations.
alembic upgrade head 
nevermind, still doesn't work.
I don't see primary key created anywhere in the migration you've sent, I'd make one myself in a seprate migration
Probably like this:
def upgrade() -> None:
op.create_primary_key("pk_known_planets", "known_planets", ["user_id", "planet_id"])
def downgrade() -> None:
op.drop_constraint("pk_known_planets", "known_planets")
If you run alembic revision it should create a new empty migration
I mean I can create a migration manually if I have to but I'd rather models.py be accurate
Alright, creating a brand new table entirely, let's see if alembic actually creates a migration for this...
@paper flower
class KnownPlanets2(Base):
__tablename__ = "known_planets2"
user_id = Column(Integer, ForeignKey("users.id"), primary_key=True)
planet_id = Column(Integer, ForeignKey("planets.id"), primary_key=True)
random = Column(Integer, default=6)
__table_args__ = (
#TODO: a primary key for two columns doesn't work
#PrimaryKeyConstraint("user_id", "planet_id", name="pk_known_planets"),
PrimaryKeyConstraint("user_id", "planet_id"),
{"mysql_collate": "utf8mb4_unicode_ci"},
)
creates
"""create test known_planets2
Revision ID: 6bbe319e7f99
Revises: 4e7c01db8a2a
Create Date: 2025-01-18 13:10:57.305330
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = '6bbe319e7f99'
down_revision: Union[str, None] = '4e7c01db8a2a'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('known_planets2',
sa.Column('user_id', sa.Integer(), nullable=False),
sa.Column('planet_id', sa.Integer(), nullable=False),
sa.Column('random', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['planet_id'], ['planets.id'], ),
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
sa.PrimaryKeyConstraint('user_id', 'planet_id'),
mysql_collate='utf8mb4_unicode_ci'
)
# ### end Alembic commands ###
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('known_planets2')
# ### end Alembic commands ###
Seems correct
But you already have an existing table and have to kind of not delete it 😅
I didn't delete a table
Also does it seem correct? ther's no reference to primary keys in the upgrade() section
There is sa.PrimaryKeyConstraint('user_id', 'planet_id')
Oh. Sorry, my eyes have been staring at my terminal so long that they're losing their effectiveness.
Yeah, this is bizarre. for the actual real table it seems to be refusing to change it to a composite key.
Is there a way of forcing that?
note that there is no data I care about...in fact I am completely destroying the database and creating a new one from scratch each time.
I could create subsequent migratiosn that drop the table then recreate it?
Alembic doesn't seem to support autogenerating/altering primary keys right now, just write a migration for it I think 🤔
Honestly just not the best practice, no point in using migrations then
If you're just developing it's fine to just remove all the migrations, drop the db and make new one + migrations
what else would i be doing but just developing?
I'm not maintaining a production database if that's what you mean
dropping production database 
well, thanks
I am so confused by migrations.
I had aniother question here on new years even about all of this, and you had actually answered me.
This was my solution lol:
#!/bin/bash
DB_ADDRESS=127.0.0.1:3307
export DB_ADDRESS
set -e # Exit immediately if a command exits with a non-zero status
MIGRATION_MSG=${1:-"default migration message"}
# Stop and remove any existing container
docker stop dev_db || true
docker rm dev_db || true
# Start a fresh database container
docker run --name dev_db \
-e MYSQL_ROOT_PASSWORD=password \
-e MYSQL_DATABASE=game \
-e MYSQL_USER=user \
-e MYSQL_PASSWORD=password \
-p 3307:3306 \
-d mariadb:10.5
# Wait for the database to initialize
echo "Waiting for the database to initialize..."
sleep 10
#until docker exec dev_db mysqladmin ping -h localhost --silent; do
# sleep 1
#done
echo "Database is ready!"
#rm -rf alembic/versions/*
# Ensure Alembic is synced to the current state
alembic stamp base
alembic upgrade head
# Generate a new migration based on models
alembic revision --autogenerate -m "$MIGRATION_MSG"
# Apply the migration
alembic upgrade head
docker stop dev_db || true
docker rm dev_db || true
chatgpt did help me but it took a LOT of arguing because AI is terrible and forgets everything after you told it.
The alembic stamping stuff makes no sense to me
its kind of similar to git
alembic stamp: moves the head/pointer to a particular revision/migration. It uses this to know where to start the migrations from
alembic upgrade; Applies those changes
also u might prolly wanna migrate the data too alembic is only schema migration afaik
thank you
Hey guys, does somebody know if I can visualise results of sql queries in qgis?
With Oracle maybe, idk about the others
it seems that there is an option but it doesnt work
I choose add layer and its doesnt add it
Hey everyone
I am trying to create a datacube
With like time lat lon and around 9-10variables values at that particular time
Which db handles Multidimensional data pretty well?
I read about scidb
Dimension of data will be like this lets say time = t lat = (200,200) lon = (200,200) and all the variables will be of (200,200)
Currently I tried creating a cube in hdf file
By appending each data to it sequentially and mapping to that particular time and I am able to query it as well for olap operations
On my system hdf files are working great
But I belive if we upload it to server and multiple user query it
Then it will not be the best choice right?
Or should I try to create my own rdbms?
Found something good
Postgresql with times Caledonia and postgis
Hi to all
anyone knows how to get rid of this error in sqlalchemy.
sqlalchemy.exc.InvalidRequestError: Object '<User at 0x7f9df97cb710>' is already attached to session '12' (this is '13')
I tried clearing database removed cookies close and run the server but this still remains the issue.
Instead of:
db_session.add(s)
Do:
current_db_sessions = db_session.object_session(s)
current_db_sessions.add(s)
https://stackoverflow.com/questions/24291933/sqlalchemy-object-already-attached-to-session
That's not a good fix imo? Just don't use multiple sessions in one context without a need for that.
@novel crest can help with that
?
Why do you essentially ping random people?
Goodmorning could someone tell me what the difference between the two queries pictured. The query at the top is mine and I’m retuning a 0 value . I’m not sure what I’m missing …
the backticks?
i am assuming they have a column with the elaborate name Elementary, Middle... which is signified using backticks to account for space and commas in the name
meanwhile you just use single quotes which makes it a string
Thank you !
How can I download data automatically everyday from website (only specific entries) and connect it to database?
I tried website scraping, API, but I think it's not efficient due to website policies
!rule 5
5. Do not provide or request help on projects that may violate terms of service, or that may be deemed inappropriate, malicious, or illegal.
if they don't allow downloading their data programmatically, then downloading it would be against their Terms of Services
Ask their administrators for efficient way to load. They are supposed to be providing API if they are willing to share it
Thank you. Can you do it with Download Station.
Wrote this up yesterday. I've fallen down enough pits over the years connecting to and querying databases in higher-volume applications that I wrote a howto on connecting and using an RDS-style db (with a read replica) with FastAPI, SQLAlchemy, and asyncpg. https://tableroq.substack.com/p/production-python-1-setting-up-fastapi No paywall. I consider good database connections a community service 😛
Hey everyone, I need some help with my logic in Node.js and Mongoose. The issue is that when I create a coinflip and the logic removes the items, the items are removed once the session comits (this lets the items still be in the inventory for somewhile), which works fine. However, if I try to create a second flip while the first one is still being processed (using the same items), I get this error:
errmsg: 'Caused by :: Write conflict during plan execution and yielding is disabled. :: Please retry your operation or multi-document transaction.'
Like it doesn't let me use the items twice which is also fine but is this error something to worry about? and i also don't like how it returns an internal server error then it should be more a "you cannot use those items" (this all while the first flip is creating)
:incoming_envelope: :ok_hand: applied timeout to @honest cairn until <t:1737928549:f> (10 minutes) (reason: duplicates spam - sent 4 duplicate messages).
The <@&831776746206265384> have been alerted for review.
how do i insert an empty stuff inside a foreign key?
like, no value
i have 1 database column and i want to insert empty value there
i don't think u can do that
You can have null if your foreign key column is nullable
is it nullable by default?
the 4 foreign keys?
because i need to make it 3 slots
and also i have little knowledge about sqlite
i only know the basic of the basic
You could have a member_soulmate table to allow for any number of soulmates
CREATE TABLE IF NOT EXISTS member_soulmate (
member_id INT,
soulmate_id INT,
PRIMARY KEY (member_id, soulmate_id),
FOREIGN KEY (member_id) REFERENCES artist(member_id),
FOREIGN KEY (soulmate_id) REFERENCES artist(member_id)
)```
You can then query all the soulmates of the member_id without being stuck on having a certain number @pearl lodge
and when it's 3 stuffs?
like there is the person, the soul mates and finally the lover
member_id to lover would be a seperate table from member_id to soulmate
These tables linking 2 things togheter are often called associative tables or junction tables
They are used to represent many to many relationships like these
i see, two tables, even thought sounds unescessary creating two more, but since you are more experienced than me you know something i don't know
my plan it's to be 3 soul mates and 1 lover for each member,
but i can see the thing you do
If it's always going to be a static number you can have it in one table
guess you mean like compare to each other, and when inserting 1 it inserts two, or...?
It would allow you to enter any number of soulmates, from 0 to infinity
Hey all.
Anyone have any suggestions for a solution to the following.
I have a an Airflow instance on premise that is being asked to sync data between on premise MSSQL and Azure SQL server.
It is almost entirely ELT, with little/no transformations happening. Mostly we follow a pattern of water marking the tables or truncate and load.
Our solution is very complicated and relies on checking set differentials between pyodbc row objects and I want to get away from this.
I have explained to management that we should not be loading the data into memory (Airflow is not a compute engine) but it has largely been ignored/not prioritized.
Is there a built in package or library/tool that can handle this?
The main issue is we are forbidden from having azure simply pull in the changes via cdc or azure data factory due to security reasons.
I think the easiest path is have airflow call SSIS jobs from the on premise servers, but was curious if there is another way.
Thanks
Tbh we just load stuff into memory for most stuff, if it is larger than what airflow can do... Then it normally becomes a spark job. Although you can also use a docker image and have airflow spawn the docker image into a compute engine of your choice
Thanks.
Yea, seems like like a reasonable choice. I might have to push to try and horizontally scale it out and add additional workers on premise. Do you have any thoughts on using SSIS to just push the changes from on premise to azure?
I can't say, we're AWS infra, haven't work enough with Azure to say
Fair enough. Appreciate the time.
Currently using MongoDB to store products, their information and historical data for each product. the historical data starts getting bigger and even with a reasonable amount of indexes, query times are going up.
Should I move the historical data to a different collection since they are less frequently requested? or would sharding be the right approach?
You have several solutions, maybe moving to a more friendly time-series database is the most difficult but sanest choice. Having hot-data collections means that you'd need to constantly re-evaluate what is hot and what is not, an execute accordingly. Sharding can also help you, even though it's not the easiest thing in MongoDB.
I don't know your data use case but MongoDB overall struggles to scale at some point, and sometimes their solution is to throw more machine ($$$) to the problem @radiant estuary
working on building an app for pantry management, recipe management, and shopping list management. would I be best off with SQLite, Mongo, DuckDB, or something else entirely? I have very limited experience with Oracle SQL and Mongo.
just go with SQLite to start
I do intend to include a paid feature later down the line where I can do price tracking and/or ai-generated recipes "using what you have"
I may mention at this point I'm also doing this as an exercise in learning prisma, I understand there's a python library for that?
!pip prisma
or would I be better off just using typescript since that's what it's designed for?
I don't know
time-series is something I avoided early while I shouldn't have. I will actively read more on it.
The use case is commerce products and tracking of changes on different sites (as simple as I can put it)
Would you recommend a different NoSQL database? I don't have enough understanding of timeseries to consider if I should redesign everything to move to cassandra or smth
I'm biased in many fronts, I think non relational databases are not a very good model. So I would personally not recommend noSQL, even though some folks out there solve cool problems with them (while also spending quite some $$)
Might not be the right place to ask but I have an issue where pd.merge_asof doesn’t work for datetimes and my df is at 6000x24 already so I’m thinking Postgres might have the solution for me. Can someone help me understand how the timespan type works in Postgres ?
Since timespan is not supported within pandas
Is it defined by two different datetime columns ‘start’ and ‘finish’?
I’m very green to using databases almost always use df exclusively
postgres timespan/interval is similar to python's datetime.timedelta, it does not have a start or finish date, but a duration e.g. one day
can someone help me its easy one
create table Student( SID char(4), Name varchar(20), RN int, Batch char(4) );insert into Student values ('S001','kristal',1,'2081'), ('S002','Anish',2,'2082'), ('S003','Radhesh',3,'2083'), ('S004','Dipesh',4,'2084'), ('S005','Pradip',5,'2085'); ALTER TABLE Student ADD CONSTRAINT PK_student PRIMARY KEY (SID);
why i am getting error while making primary key
other works fine
What's the error? 
is there a way to have a share auto increment id between 2 tables, so that 2 rows in each table will never have same id? sqlite
assume im never gonna manually insert an id
why do they have to be auto increment
ok, if its not auto increment then what else?
uuid4 for example
if you need to sort, you can sort by an extra created_at, or use an ULID which is lexicographically sortable
your two tables will most likely never collision, for example at CrateDB; as it is distributed, uuid are often used as ids, also generated columns with timestamps
i need it to be a 32 bit int
why?
its because the tables that need to share an id are very differnt to each other, sure i can make it work by combing the tables into one, such as making certain fields as another table, or making it nullable. but its just alot of work just because i need the ids to never share
I think talking about the bigger picture of what you need to accomplish is what is being asked of you
This is feeling like a XY problem
anyways i came up with a shitty solution
i made a table,
db.execute("""CREATE TABLE IF NOT EXISTS shared_ids (id INTEGER)""")
db.execute("""INSERT INTO shared_ids SELECT 1 WHERE NOT EXISTS (SELECT 1 FROM shared_ids)""")
then to get an id
UPDATE shared_ids SET id = id + 1 RETURNING id
But if they're very different from each other why do they need to share the same id sequence?
Is the psycopg2 library the best option? it's just the first one i found and already implemented into my project and i don't know if it's worth it to search for other ones.
Yes, psycopg is fantastic. You likely want psycopg. It's version 3 of the same library. Shouldn't take much to migrate to it.
Guys, want to ask, if anyone use SQLAlchemy, how to limit the number of records?
Like this SQL:
SELECT * FROM Users LIMIT 3 OFFSET 1
Also how to offset
I have no idea if this'll help but I found a stack overflow question that seems related https://stackoverflow.com/questions/13258934/applying-limit-and-offset-to-all-queries-in-sqlalchemy
stmt = stmt.limit(3).offset(1)
Can someone explain why
sql = """
INSERT INTO mathProblemSolver_mathproblemsolve (imageLink, answer, answerPlainText, user_id, status)
VALUES (%s, %s, %s, %s, %s)
"""
data = (self.imageLink, self.answer_uuid, self.answerPlainText, self.userID, 1)
cursor.execute(sql, data)
can avoid skibidi kid run sql injection rather than
sql = f"""
INSERT INTO mathProblemSolver_mathproblemsolve (imageLink, answer, answerPlainText, user_id, status) VALUES
('{self.imageLink}', '{self.answer_uuid}', '{self.answerPlainText}', '{self.userID}', 1)
"""
cursor.execute(sql)
ChatGPT answer doesn't make sence for mehttps://chatgpt.com/share/67a0e6b1-e9a4-8009-92bc-6d4ec26aeb09
!user
You are not allowed to use that command here. Please use the #bot-commands channel instead.
Don’t manually merge values to a query: hackers from a foreign country will break into your computer and steal not only your disks, but also your cds, leaving you only with the three most embarrassing records you ever bought. On cassette tapes.
If you use the % operator [or f-string] to merge values to a query, con artists will seduce your cat, who will run away taking your credit card and your sunglasses with them.
If you use + to merge a textual value to a string, bad guys in balaclava will find their way to your fridge, drink all your beer, and leave your toilet seat up and your toilet paper in the wrong orientation.
At a glance that response looks like about what I would say, is there a particular part that doesn't make sense?
Don't know why ChatGPT say use % operator can avoid sql injection instead of f-string
Because the adapter converts the types from Python to SQL, instead of sending them to the database as strings.
Some adapters, like psycopg, even send the variables separately so they're never evaluated as SQL.
That sounds better
at least the con artists wont steal my credit card
hey guys pls checkout a tool i made to check conflict serializability of a schedule
https://github.com/coderatul/conflict-serializable-validator
Hi, I'm getting some error. Here is my code:
def get_code_verifier(discord_id):
data = redis_client.get(discord_id)
if data:
data_dict = json.loads(data) # Error underlines "data"
return data_dict.get('code_verifier')
else:
return None
```Error:
Pyright: Argument of type "ResponseT" cannot be assigned to parameter "s" of type "str | bytes | bytearray" in function "loads"
Type "ResponseT" is not assignable to type "str | bytes | bytearray"
Type "Awaitable[Any]" is not assignable to type "str | bytes | bytearray"
"Awaitable[Any]" is not assignable to "str"
"Awaitable[Any]" is not assignable to "bytes"
"Awaitable[Any]" is not assignable to "bytearray"
Libraries: Redis Cache
Has anyone used postgresql with postgis and timescaledb for rastor data?
Really need some help on how to transfer my data in hdf files to postgresql
hey guys, just was wondering, how useful/necessary are databases for machine learning? specifically computer vision
if im correct, pandas is centered around databases (or is it just data frames that i'm confusing databases with) so would that be enough or would sql/some dbms also be helpful to have under my belt
also what are some pros and cons of each of the 4 dbms types (network, hierarchical, relational, and oop), asking cause i want to know which type is best for me to learn
ignore network, focus on relational databases (SQL)
not even sure about what you mean by hierarchical and oop
with machine learning you'll frequently see people stick with plain files instead of properly storing data in a database during experimentation and development, but for production you'll almost always need to use a database regardless of what you are building
DataFrames != Databases
data frames are just a form of representing data, a little similar to spreadsheets
If you want to touch anything related to data, it's fundamental to have a basic understanding of SQL
Being familiar with the pandas API is also a good idea if you plan to work with data in Python, some other libraries straight way copy their API too. Start with the pandas official User Guide
hierarchical is the tree like structured dbms, and then oop is object oriented programming, though i'm not sure how things would actually be organized with that one
also which relational dbms is it best to go with, or does it not matter
currently i'm learning tsql
also what are network dbms's typically used for, i'm still sticking to relational but just curious as to their purpose that differs from the usage of relational dbms's
is there any idea on how to establish gateways in powerbi?
assuming you mean Graph Databases such as Neo4J, tbh just see https://neo4j.com/use-cases/
as a whole, they focus on how the data is connected rather than making aggregations
e.g. list all of user X friend's friends, find the shortest path between X and Y, find all Z things connected to things matching Y filter connected to X main thing
"This really depends. From what I've done so far on Power BI, I see two types of gateways: one that provides a “connection” to a file on your local PC (like an Excel spreadsheet) and the other to any database (MySQL, PostgreSQL, etc.). For the local PC, you need to download the gateway configuration onto your machine and point it to the file (the gateway config installl on Microsoft).With databases, you just need to upload a Power BI file with a connection to the database and then configure it again in the Power BI web interface (Fabric). Sorry if my English isn't perfect, but feel free to ask me any questions
hey guys, trying to change the path of the database storage for postgresql
has anyone done this before?
hi folks, can anyone recommend any beginners books that will help me build towards my goal of applying for my first job as a data analyst? i have a strong understanding of mathematics and calculus already and i am currently teaching myself python - i've just started to realise exactly how broad this field really is. ty!
any o'reilly book is usually good most of time
Python for Data Analysis: Data Wrangling with Pandas, NumPy, and Jupyter is a good book start I think
that book is on my shelf somewhere i will have a look through it, ty
Easy peasy lemon squeeazy https://hub.docker.com/_/postgres
Just set different hostpath in docker volume 😏
$ docker run -d \
--name some-postgres \
-e POSTGRES_PASSWORD=mysecretpassword \
-e PGDATA=/var/lib/postgresql/data/pgdata \
-v /custom/mount:/var/lib/postgresql/data \
postgres
Replace /custom/mount with desired host path
Does anyone know if there's a way to find the quantiles of a row of a pandas dataframe? I was given a dataframe where the rows are years, the columns are different countries and the data is population. Have to have the 10th percentile of the data of a specific year.
Unsure if this is the right chat for. This question tbh.
!d pandas.DataFrame.quantiles
No documentation found for the requested symbol.
!d pandas.DataFrame.quantile
DataFrame.quantile(q=0.5, axis=0, numeric_only=False, interpolation='linear', method='single')```
Return values at the given quantile over requested axis.
database bros and girls, you sure ric falir won 16 championships????
oh damn what i nice command
python bot is insane
!d pandas.DataFrame
why did the bot react with a trash can lmfao
!d numpy.array
numpy.array(object, dtype=None, *, copy=True, order='K', subok=False, ndmin=0, like=None)```
Create an array.
Hello
I would like to have a related (one to one) object eagerly loaded whenever I retrieve an object from the db using SQLAlchemy. For example, each User has one Role. If I do user = session.get(User, 12), I would like the role attribute to be eagerly loaded, so if I do user.role.permissions afterward, the db will not be hit.
I'm porting from a manual approach in another language which used:
"select " select-columns " "
"from users left outer join roles on users.role_id = roles.id "
"where users.organization_id = $1 and lower(username) = $2"
ChatGPT stated, "Setting lazy="joined" in relationship() means it will always be eagerly loaded by default."; however, it unsurprisingly lazily loads. Not sure why it expected "lazy" would indicate "eager".
Hmm... never mind, it seems to be working now - sorry for the noise!
Please
- Don't use chatgpt
- Read the docs (sorry) 😅
but on more productive note - settinglazy=on arelationshipinstance is probably not what you want in most cases, unless you're always, or most of the time access user's roles when fetching it from the database.
You can specify loading options on a query level:
stmt = select(User).options(joinedload(User.roles))
user = session.scalar(stmt)
# Or if you're using .get
user = session.get(User, ident=12, options=(joinedload(User.roles),))
Actually, ChatGPT nailed this one. It was me that screwed something up for a bit. As I mentioned in my original post, I always want the user's role to be loaded everytime the user is loaded.
I mostly tend to add it on individual queries, depending on a project there could be a lot of tables, keeping all of the relationships in check could be a challenge
Also with async lazy loading isn't a thing, so I'm forced to add a loading option somewhere 😅
Sure, I expect most relationships won't be eagerly loaded like this.
Is it possible to do the opposite i.e. default to eager, as above, but on rare queries where I don't need the role, to turn off the eager loading in the query?
I think maybe with this? https://docs.sqlalchemy.org/en/20/orm/queryguide/relationships.html#sqlalchemy.orm.Load.noload
I used defer and undefer that way, one of my models in a project has a very big text field 😅
e.g.
class TextFragment(Base):
contents: Mapped[str | None] = mapped_column(deferred=True)
stmt = select(TextFragment).options(undefer(TextFragment.contents))
I think you can do the same with relationships
That would be awesome.
Fortunately, this first app isn't very performance sensitive, so I have some time to read up on SQLAlchemy techniques.
Hi, how are you
I explained a problematic I have regarding databases in the following thread https://discord.com/channels/267624335836053506/1338891570249990197
Any help is much appreciated
Hi, i dont know if this is the coreect chat but anyone know i site to upload python code and work 24/7
what's the most commonly used dbms
I don’t understand how data frames work
can anyone recommend a database very very similar to mongodb but its very very very fast?
redis is another nosql db
Postgres with json fields
Ah a typical one depends if it uses flask then you can use python anywhere
Ping me plz
Hi Guys I was wondering if anyone has some interesting ideas for a GUI im working on. It's an app that reconcilies data between sources. I added Radar charts, time progression, filtering capabilities but it's all in ktinter and custom tkinter
I wanted to make something similar to ios ui but i'm pretty new
Where/how do you host a data base?
Could someone shed some light on this lmao? I don’t know much about databases.
You missed the post with the community note
There's not much to know
- Elon made really dumb statements that show he has no idea WTF he's talking about
- Someone called him out for it
- Elon retaliated against being called out by calling people slurs
that's pretty much it
They're programs that run on your computer, just like Python.
which is better, mysql or postgresql
PowerBI is a good starting point for that
what would a good data pipeline roadmap be
i have a roadmap that i'm following for sql specifically but that's only a part of dbms, and dbms itself is also only a section of data pipelines
i'm looking into learning at least the basics of every part of data pipelines so that i have a baseline of each part, though the part i'll be specializing in is the end/data usage part, specifically machine learning, so i want to see if later on i can try to create a full data pipeline for my own ml applications
guys i already start a python project but i confused witch Database should i use MySql or PostGreSQL ?
Postgres
Sqlite3 is for the win for pet projects
SQL simplified to database as a file
It has unit testing code 679 times more than working code
Quite quality one choice
For personal use or small projects Turso works quite well https://docs.turso.tech/sdk/python/quickstart
Get started with Turso and Python using the libSQL client in a few simple steps.
how much of a difference does it make whether the database is a file or within a server
it is just simpler to manage db when it is a single file. No extra instance of smth is running that needs to be managed / upgraded
Simple scp to back it up
No need upgrading it, no need knowing docker for its easy running.
Your sqlite3 database is tied to drivers/code of your app directly.
Wayyy more convenient in a single file and not having to setup anything.
No permissions to worry about either, user setup, etc.
@deft apex and considering that people often use ORMs anyway
At ORM level of pet projects there is in code no difference between using Sqlite3 and postgresql/mysql. (at least if u aren't using Constraints or Dialect specific stuff that is present only in Postgresql/Mysql and etc)
Yet... Sqlite3 is way easier to run
what if i already have it set up
yeah true
i just went with mysql cause i like that you can run individual queries instead of having the run the whole file
i could also switch to postgresql though
cause the download comes with it
Also Sqlite3 database is secure by default 😄 No public access is possible, no accidental exposure of a passwordless db as people often do with postgres/mysql/mariadb
Sqlite3 is accessable only over the file, no internet access
just would have to make a postgresql server
the free version only allows 2 sql servers and 2 nosql servers though
for sql that's fine, but for nosql it sucks cause then i can only try out 2 among network, hierarchy, and oop dbms types
True, but I don't think I've met constraints complex enough yet that SQLalchemy can't abstract them afaik.
You might as well use it I guess. Do you need multiple applications to be able to query it over the network?
nah i don't think so
i'll be honest i don't even know what that means
i know nothing
still learning basic sql
though i could probably finish learning all of it in a day or two
out of these 3 which 2 are best, mysql, postgresql, and mariadb
It's up to you to choose based on your own requirements. Best depends on what you need and how you need it
hey anyone here have experience with qt?
Anybody read sql for smarties? is it worth reading?
I am here
@tough dew i texted
Hi
In what ways can I make my code faster when working with databases (aside from trying to keep as much of the database part in sql)?
It might help to talk about what you are doing specifically, the scale of the data, and how it is consumed. Are you trying to make queries faster? Rendering? Need a lot more info
Im just talking in general. Assume Ive got a very large dataset I need analyse. What do I need to do to make queries run faster?
Having Indexes 😏
I am getting the weirdest error when trying to use flask migrate. Can someone help?
flask db init works but flask db migrate -m "Initial migration." is causing the error
https://paste.pythondiscord.com/ZFCA
I also posted this in web development
the errors says stuff along side of (with high likelyhood)
- File does not exist
- Or broken in file permissions
- Or your sqlite3 db is locked because of some corruption
When u connect via pgadmin, u can see active sessions with which queries they ran
Can i get same info via Postgres query / in a raw way with PSQL?
i need easier interface to find query of interest to me
having them all in a raw way will help me easier grabbing needed one by notepad search
found, at least
select pid,
usename as username,
datname as database_name,
query,
application_name,
backend_start,
state,
state_change
from pg_stat_activity
have any of you guys used neo4j, if so, did you use it for any ml related tasks?
I'm using MariaDB v11 and I'm trying to implement a search type functionality on the clients_test table that is defined as
clients_test (first_name TEXT, last_name TEXT, number TEXT, email TEXT)```
on the UI multiple inputs are provided (first & last name, number, email), they can fill in any field and all fields should be used to find all rows matching partial/full data. I'm not really sure on how to implement this in SQL or if there's a better way to? something I tried to do is
```sql
SELECT * FROM clients_test WHERE first_name LIKE "jak" AND last_name LIKE "" AND number LIKE "" AND email LIKE "";```
AND
```sql
SLECT * FROM clients_test WHERE SUM(SUM(first_name LIKE "jak") + SUM(last_name LIKE "") + SUM(number LIKE "") + SUM(email LIKE "")) > 0```
all the values are parameterized with `?`, above `""` values are just examples for empty inputs. `jak` is partial for `jake` in this case.
1st doesn't return the correct (any) columns, i assume because some columns are empty (`""`). 2nd is syntax error but i felt maybe something like might be more plausible? not sure how to implement that properly or if there's a better / simpler / more efficient way to do this. can't find much searching online either (might be wrong search terms?). any help is appreciated!
and i just realised what was wrong with the 1st query... i didnt have % in the input 
gonna leave the message in case someone suggests a better awy
what are some good projects for working with databases and sql
My latest project is a dog treat store with an inventory and cart and stock tracking with user/inventory management
Has anyone here worked with dynamodb?
So here's my scenario
- I need to get tasks that were created this month I have about a million tasks or more waaay more. But I want them sorted by their due date
My current approach is to have their createdDate in the SK so that I can get the ones created in this month Instantly that will shorten my dataset to about 50,000 or less then I will sort them in my application manually
Is this a good approach i can't think of any other solution 😭😭
Hi guys, could you please tell me if my approach is correct? I have a table in postgres with uuid and code and each of these columns must contain only unique entry and I wanna check by insert on conflict do nothing ( because it's concurrently safe ) that these requirements are met. So, I'm going to create unique constraint on uuid and another unique constraint on code. And when I try to insert I will get unique constraint violation error, if either one or both not unique. Sounds ok?
why not use compound keys
I think they mentioned that they want to have uniqueness across single field too, so having two duplicate ids or codes shouldn't be valid?
Their solution sounds correct though
yep sounds correct also depends on whether op also wants like combinational uniqueness which is what i often finding myself in need of
Combinations would be unique too
they'd literally have both columns under their own unique constraint, making duplicate combinations impossible
yes u would have like only a single instance of (x1, y1) one to one mapping compound keys can be one to many or many to many
Btw, better to not make composite primary keys if you're going to refer to that table from somewhere, since you'd have to create 2 columns there, but it's a valid case if you have some kind of multitenant system, e.g. if you have customer_id on each of your tables
an example of how i use it might go like this
user table (userid, name email, etc)
roles (role id, name, scopes etc)
user role association (user id, role id)
If you have a m2m table (association) it's fine to use composite pkeys there since it's not going to be referenced from somewhere in 90% of cases, also generally sounds like the "correct" solution on how to make one
true
i wish sql alchemy had lazy loading tho without me performing joins everywhere iirc they dont support async lazy loading when accessing these relationship properties from the model
Why would you use lazy loading? 🤔
It's inefficient
also generally leads to N+1 Query problem
i set lazy="joined" iirc
That's eager loading
which loads it along with the main model
yeah
sometimes i may or may not need that property depending on my endpoints along with the main model
I wouldn't configure it on models, you can specify the loading you want on individual queries:
stmt = select(User).options(joinedload(User.profile), selectinload(User.posts))
honestly i feel like in most cases i could prolly just do without sqlalchemy, just asyncpg and writing my own queries
https://paste.pythondiscord.com/EAVQ
why am i getting an error message saying collecteddates/products arent defined when they clearly are
(imports and cursor and stuff are in the code just not in the snippet)
hi
hello guys checkout a python tool using which one may visualize and learn about concurrency control system in DBMS
conflict serializeable schedule validator
https://github.com/coderatul/conflict-serializable-validator
Hello im trying to initialise a table in my database but it keeps giving me an error when i use autoincrement anyone knwo whats going on?
Primary key before auto increment . Example https://stackoverflow.com/questions/7905859/is-there-auto-increment-in-sqlite
And username unique isn't right either
Try the example from the so link, so you can see how it's supposed to look
alright imma try that ty
hey i'm doing a chatapp and i would need some recommendation here, i'm starting to need to handle the data sent by a user and then send them to another. this means that i now need to store those data somewhere, what would be the best way to store them
in a efficient/secure way
?
i come from he #python-discussion, just to say that i know nothing about that exept that it exist smthg named my SQL
researching shows that postgresql is recommended for starters or mysql if you care so much about speed
I like SPEEEEEEEEEEEEEEED
lol
complexity doesn't bother me, just not a PHD
Python comes builtin with a connector for a database called SQLite. I suggest starting there. The docs show how to get started with writing and reading data. https://docs.python.org/3/library/sqlite3.html
SQLite sounds a bit too little for my ego lol
Yep, you could always migrate later if you needed too. (you probably won't need too)
ok good i'm lookin into that
That's a big ego if it's bigger than most used database in the world! 😉
my ankles did indeed broke while ago
if i'm stuck i suppose ill come here ask instead of discussion now
Is there anyway I can get a specific data from a db for a specific person
(For example my login database has the username and password)
(And the detail database has the email, credit card etc)
I want it to get the credit card of a certain username/person however I don’t know how to get it to know which user is using the tkinter app/I can’t really get if by using a unique identifier (username) as it’s in a different database
I have username as a foreign key in the database but confused if that would help my situation
I’m using python, tkinter, sqlite3
If there is no common key between the tables then it would not be possible. I suggest adding a user_id field to your detail table.
How would I make it assign the data to the user id which matches the user that’s currently logged in
What information do you have on the currently logged in user? Use that to get the id.
Anyone here use sqlalchemy? I'm loving it
First ORM I've used
Any other good ORMs for python?
No, I use MySQL and PostgreSQL
Good, I'll test it
I ❤️ SQLAlchemy
It's just an ORM it's not the database itself, you can use MySQL or postgresql with it