Skip to content

Instantly share code, notes, and snippets.

import traceback
from contextlib import contextmanager
from sqlalchemy import create_engine, inspect, Column, Integer, String, DateTime, ForeignKey, Table, MetaData
from sqlalchemy.orm import sessionmaker, relationship, aliased, contains_eager, scoped_session, backref, mapper
engine = create_engine('sqlite:///:memory:', echo=True)
metadata = MetaData(bind=engine)
objecttypes = Table('objecttypes', metadata,
Column('id', Integer, primary_key=True),
Column('name', String)
@funseiki
funseiki / reproduce_error_classic.py
Created December 4, 2020 16:20
NULL Identity Key Error
import traceback
from contextlib import contextmanager
from sqlalchemy import create_engine, inspect, Column, Integer, String, DateTime, ForeignKey, Table, MetaData
from sqlalchemy.orm import sessionmaker, relationship, aliased, contains_eager, scoped_session, backref, mapper
engine = create_engine('sqlite:///:memory:', echo=False)
metadata = MetaData(bind=engine)
objecttypes = Table('objecttypes', metadata,
Column('id', Integer, primary_key=True),
Column('name', String)
@funseiki
funseiki / reproduce_error.py
Created November 19, 2020 23:09
Script to reproduce SQLAlchemy error
import sqlalchemy as sa
from sqlalchemy import create_engine, inspect, Column, Integer, String, DateTime, ForeignKey
from sqlalchemy.orm import sessionmaker, relationship, aliased, contains_eager, scoped_session
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class ObjectType(Base):
__tablename__ = "objecttypes"
id=Column(Integer, primary_key=True)
@funseiki
funseiki / eager_loading.py
Last active August 12, 2019 22:47
SqlAlchemy Eager Loading Example Issue
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.ext.declarative import declarative_base
def buildConnectionString(db_type, server, port, name, user, password):
# We're using an in-memory data
if db_type == "sqlite":
return "sqlite://"
@funseiki
funseiki / test_oracle_and_pg.py
Created December 20, 2018 22:28
Many To Many Error Example
import yaml
import datetime
import sqlalchemy as sa
import sqlalchemy.orm as saOrm
class Run(object):
def __init__(self, **kwds):
for key in kwds:
setattr(self, key, kwds[key])
return
@funseiki
funseiki / example_fruits.py
Created July 13, 2018 04:42
SqlAlchemy AttributeError for Unmapped Subclasses
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.associationproxy import association_proxy
metadata = MetaData()
fruitTypesTable = Table(
'FruitTypes', metadata,
Column('type_id', Integer, primary_key=True),
Column('name', String(256))
@funseiki
funseiki / main.py
Created June 19, 2018 17:50
Multiple polymorphic identities
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.associationproxy import association_proxy
from utils import deco, hashFunc, queryFunc
metadata = MetaData()
fruitTypesTable = Table(
'FruitTypes', metadata,
Column('type_id', Integer, primary_key=True),
@funseiki
funseiki / Dockerfile
Last active March 1, 2018 22:48
Docker Compose Network Interaction Example
FROM python:3
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
ENV FLASK_APP=/app/app.py
import asyncio
import threading
import time
import queue
from aiohttp import web
class Worker:
def __init__(self, queue):
self.queue = queue
self._t = threading.Thread(target=self._doStuff)
@funseiki
funseiki / ICloud.java
Created March 8, 2015 23:41
Parse to Twitter Digits validation
// Uses retrofit to generate an implementation
public interface ICloud {
static String serviceProviderHeader = "X-Auth-Service-Provider";
static String credentialsAuthorizationHeader = "X-Verify-Credentials-Authorization";
@GET("/verify_credentials?provider=digits")
void verifyCredentials(
@Header(serviceProviderHeader) String serviceProvider,
@Header(credentialsAuthorizationHeader) String authorization,
@Query("id") long id,