Create Multi-Column Index in SQLAlchemy

Defining Multi-Column Indexes Using ORM Declarative Mapping

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
from sqlalchemy import create_engine, Column, Integer, String, Index
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
engine = create_engine('postgresql://username:password@localhost/mydatabase')
Base = declarative_base()
class Employee(Base):
__tablename__ = 'employees'
id = Column(Integer, primary_key=True)
last_name = Column(String)
first_name = Column(String)
department_id = Column(Integer)
__table_args__ = (
Index('idx_employees_last_first', 'last_name', 'first_name'),
)
Base.metadata.create_all(engine)
from sqlalchemy import create_engine, Column, Integer, String, Index from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker engine = create_engine('postgresql://username:password@localhost/mydatabase') Base = declarative_base() class Employee(Base): __tablename__ = 'employees' id = Column(Integer, primary_key=True) last_name = Column(String) first_name = Column(String) department_id = Column(Integer) __table_args__ = ( Index('idx_employees_last_first', 'last_name', 'first_name'), ) Base.metadata.create_all(engine)
from sqlalchemy import create_engine, Column, Integer, String, Index
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

engine = create_engine('postgresql://username:password@localhost/mydatabase')
Base = declarative_base()

class Employee(Base):
    __tablename__ = 'employees'
    id = Column(Integer, primary_key=True)
    last_name = Column(String)
    first_name = Column(String)
    department_id = Column(Integer)

    __table_args__ = (
        Index('idx_employees_last_first', 'last_name', 'first_name'),
    )

Base.metadata.create_all(engine)

Creating Indexes After Table Definition

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
from sqlalchemy import create_engine, MetaData, Table, Index
engine = create_engine('postgresql://username:password@localhost/mydatabase')
metadata = MetaData(bind=engine)
# Reflect the existing table
employees = Table('employees', metadata, autoload_with=engine)
# Create the index
index = Index('idx_employees_last_first', employees.c.last_name, employees.c.first_name)
index.create(engine)
from sqlalchemy import create_engine, MetaData, Table, Index engine = create_engine('postgresql://username:password@localhost/mydatabase') metadata = MetaData(bind=engine) # Reflect the existing table employees = Table('employees', metadata, autoload_with=engine) # Create the index index = Index('idx_employees_last_first', employees.c.last_name, employees.c.first_name) index.create(engine)
from sqlalchemy import create_engine, MetaData, Table, Index

engine = create_engine('postgresql://username:password@localhost/mydatabase')
metadata = MetaData(bind=engine)

# Reflect the existing table
employees = Table('employees', metadata, autoload_with=engine)

# Create the index
index = Index('idx_employees_last_first', employees.c.last_name, employees.c.first_name)
index.create(engine)

References
https://stackoverflow.com/questions/14419299/adding-indexes-to-sqlalchemy-models-after-table-creation

Install Realm Object Server on Ubuntu 16.04

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// Ubuntu 16.04 (64 bit; 32-bit is not supported)
//It is recommended that you install the server as a normal user.
sudo apt-get update
sudo apt-get install build-essential libssl-dev
sudo apt-get install python
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.4/install.sh | bash
// Force current session to know changes or logout and log back in
source ~/.profile
nvm install --lts
npm install -g node-gyp
// Ubuntu 16.04 (64 bit; 32-bit is not supported) //It is recommended that you install the server as a normal user. sudo apt-get update sudo apt-get install build-essential libssl-dev sudo apt-get install python curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.4/install.sh | bash // Force current session to know changes or logout and log back in source ~/.profile nvm install --lts npm install -g node-gyp
// Ubuntu 16.04 (64 bit; 32-bit is not supported)
//It is recommended that you install the server as a normal user.

sudo apt-get update

sudo apt-get install build-essential libssl-dev

sudo apt-get install python

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.4/install.sh | bash

// Force current session to know changes or logout and log back in
source ~/.profile

nvm install --lts

npm install -g node-gyp
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
npm install -g realm-object-server
npm install -g realm-object-server
npm install -g realm-object-server

References
https://docs.realm.io/server/installation/manual-install

Getting Started with SQLite3 – Basic Commands

Installation

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo apt-get install sqlite3 libsqlite3-dev
sudo apt-get install sqlite3 libsqlite3-dev
sudo apt-get install sqlite3 libsqlite3-dev

Create Database

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sqlite3 newDatabase.db
sqlite3 newDatabase.db
sqlite3 newDatabase.db

References
https://www.sitepoint.com/getting-started-sqlite3-basic-commands/

Installing Apache Cassandra on Ubuntu

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
echo "deb http://www.apache.org/dist/cassandra/debian 39x main" | sudo tee -a /etc/apt/sources.list.d/cassandra.sources.list
echo "deb http://www.apache.org/dist/cassandra/debian 39x main" | sudo tee -a /etc/apt/sources.list.d/cassandra.sources.list
echo "deb http://www.apache.org/dist/cassandra/debian 39x main" | sudo tee -a /etc/apt/sources.list.d/cassandra.sources.list
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
curl https://www.apache.org/dist/cassandra/KEYS | sudo apt-key add -
curl https://www.apache.org/dist/cassandra/KEYS | sudo apt-key add -
curl https://www.apache.org/dist/cassandra/KEYS | sudo apt-key add -
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo apt-get update
sudo apt-get update
sudo apt-get update
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo apt-key adv --keyserver pool.sks-keyservers.net --recv-key A278B781FE4B2BDA
sudo apt-key adv --keyserver pool.sks-keyservers.net --recv-key A278B781FE4B2BDA
sudo apt-key adv --keyserver pool.sks-keyservers.net --recv-key A278B781FE4B2BDA
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo apt-get install cassandra
sudo apt-get install cassandra
sudo apt-get install cassandra
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo service cassandra start
sudo service cassandra start
sudo service cassandra start
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo service cassandra stop
sudo service cassandra stop
sudo service cassandra stop

References
http://cassandra.apache.org/download/

Install MongoDB as Windows Service

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
mkdir c:\data\db
mkdir c:\data\log
mkdir c:\data\db mkdir c:\data\log
mkdir c:\data\db
mkdir c:\data\log

create a file at C:\mongodb\mongod.cfg

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
systemLog:
destination: file
path: c:\data\log\mongod.log
storage:
dbPath: c:\data\db
systemLog: destination: file path: c:\data\log\mongod.log storage: dbPath: c:\data\db
systemLog:
    destination: file
    path: c:\data\log\mongod.log
storage:
    dbPath: c:\data\db

Install the MongoDB service

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install

Start the MongoDB service

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
net start MongoDB
net start MongoDB
net start MongoDB

Stop or remove the MongoDB service as needed

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
net stop MongoDB
net stop MongoDB
net stop MongoDB
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
"C:\mongodb\bin\mongod.exe" --remove
"C:\mongodb\bin\mongod.exe" --remove
"C:\mongodb\bin\mongod.exe" --remove

References :
https://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/