|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [ |
| 8 | + { |
| 9 | + "name": "stdout", |
| 10 | + "output_type": "stream", |
| 11 | + "text": [ |
| 12 | + "posts\n", |
| 13 | + "users\n", |
| 14 | + "-------------\n", |
| 15 | + "users\n", |
| 16 | + "posts\n" |
| 17 | + ] |
| 18 | + } |
| 19 | + ], |
| 20 | + "source": [ |
| 21 | + "from sqlalchemy import create_engine, MetaData, Table, Integer, String, Column, Text, DateTime, Boolean, ForeignKey\n", |
| 22 | + "\n", |
| 23 | + "metadata = MetaData()\n", |
| 24 | + "\n", |
| 25 | + "user = Table('users', metadata,\n", |
| 26 | + " Column('id', Integer(), primary_key=True),\n", |
| 27 | + " Column('user', String(200), nullable=False),\n", |
| 28 | + ")\n", |
| 29 | + "\n", |
| 30 | + "posts = Table('posts', metadata,\n", |
| 31 | + " Column('id', Integer(), primary_key=True),\n", |
| 32 | + " Column('post_title', String(200), nullable=False),\n", |
| 33 | + " Column('post_slug', String(200), nullable=False),\n", |
| 34 | + " Column('content', Text(), nullable=False),\n", |
| 35 | + " Column('user_id', Integer(), ForeignKey(\"users.id\")),\n", |
| 36 | + ")\n", |
| 37 | + "\n", |
| 38 | + "for t in metadata.tables:\n", |
| 39 | + " print(metadata.tables[t])\n", |
| 40 | + "\n", |
| 41 | + "print('-------------') \n", |
| 42 | + "\n", |
| 43 | + "for t in metadata.sorted_tables:\n", |
| 44 | + " print(t.name) # print table name" |
| 45 | + ] |
| 46 | + }, |
| 47 | + { |
| 48 | + "cell_type": "code", |
| 49 | + "execution_count": 2, |
| 50 | + "metadata": {}, |
| 51 | + "outputs": [ |
| 52 | + { |
| 53 | + "name": "stdout", |
| 54 | + "output_type": "stream", |
| 55 | + "text": [ |
| 56 | + "['posts.id', 'posts.post_title', 'posts.post_slug', 'posts.content', 'posts.user_id']\n", |
| 57 | + "['posts.id', 'posts.post_title', 'posts.post_slug', 'posts.content', 'posts.user_id']\n", |
| 58 | + "{ForeignKey('users.id')}\n", |
| 59 | + "PrimaryKeyConstraint(Column('id', Integer(), table=<posts>, primary_key=True, nullable=False))\n", |
| 60 | + "MetaData(bind=None)\n", |
| 61 | + "post_title\n", |
| 62 | + "VARCHAR(200)\n" |
| 63 | + ] |
| 64 | + } |
| 65 | + ], |
| 66 | + "source": [ |
| 67 | + "print(posts.columns) # return a list of columns\n", |
| 68 | + "print(posts.c) # same as posts.columns\n", |
| 69 | + "print(posts.foreign_keys) # returns a set containing foreign keys on the table\n", |
| 70 | + "print(posts.primary_key) # returns the primary key of the column\n", |
| 71 | + "print(posts.metadata) # get the MetaData object from the table\n", |
| 72 | + "print(posts.columns.post_title.name) # returns the name of the column\n", |
| 73 | + "print(posts.columns.post_title.type) # returns the type of the column" |
| 74 | + ] |
| 75 | + }, |
| 76 | + { |
| 77 | + "cell_type": "code", |
| 78 | + "execution_count": 3, |
| 79 | + "metadata": {}, |
| 80 | + "outputs": [ |
| 81 | + { |
| 82 | + "name": "stderr", |
| 83 | + "output_type": "stream", |
| 84 | + "text": [ |
| 85 | + "/home/pp/project/sqlalchemy/env/lib/python3.5/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use \"pip install psycopg2-binary\" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.\n", |
| 86 | + " \"\"\")\n" |
| 87 | + ] |
| 88 | + } |
| 89 | + ], |
| 90 | + "source": [ |
| 91 | + "from sqlalchemy import create_engine, MetaData, Table, Integer, String, \\\n", |
| 92 | + " Column, DateTime, ForeignKey, Numeric, CheckConstraint\n", |
| 93 | + "\n", |
| 94 | + "from datetime import datetime\n", |
| 95 | + "\n", |
| 96 | + "metadata = MetaData()\n", |
| 97 | + "\n", |
| 98 | + "engine = create_engine(\"postgres+psycopg2://postgres:pass@localhost/sqlalchemy_tuts\")\n", |
| 99 | + "\n", |
| 100 | + "customers = Table('customers', metadata,\n", |
| 101 | + " Column('id', Integer(), primary_key=True),\n", |
| 102 | + " Column('first_name', String(100), nullable=False),\n", |
| 103 | + " Column('last_name', String(100), nullable=False),\n", |
| 104 | + " Column('username', String(50), nullable=False),\n", |
| 105 | + " Column('email', String(200), nullable=False),\n", |
| 106 | + " Column('address', String(200), nullable=False),\n", |
| 107 | + " Column('town', String(50), nullable=False),\n", |
| 108 | + " Column('created_on', DateTime(), default=datetime.now),\n", |
| 109 | + " Column('updated_on', DateTime(), default=datetime.now, onupdate=datetime.now)\n", |
| 110 | + ")\n", |
| 111 | + "\n", |
| 112 | + "\n", |
| 113 | + "items = Table('items', metadata,\n", |
| 114 | + " Column('id', Integer(), primary_key=True),\n", |
| 115 | + " Column('name', String(200), nullable=False),\n", |
| 116 | + " Column('cost_price', Numeric(10, 2), nullable=False),\n", |
| 117 | + " Column('selling_price', Numeric(10, 2), nullable=False),\n", |
| 118 | + " Column('quantity', Integer(), nullable=False),\n", |
| 119 | + " CheckConstraint('quantity > 0', name='quantity_check')\n", |
| 120 | + ")\n", |
| 121 | + "\n", |
| 122 | + "\n", |
| 123 | + "orders = Table('orders', metadata,\n", |
| 124 | + " Column('id', Integer(), primary_key=True),\n", |
| 125 | + " Column('customer_id', ForeignKey('customers.id')),\n", |
| 126 | + " Column('date_placed', DateTime(), default=datetime.now),\n", |
| 127 | + " Column('date_shipped', DateTime())\n", |
| 128 | + ")\n", |
| 129 | + "\n", |
| 130 | + "\n", |
| 131 | + "order_lines = Table('order_lines', metadata,\n", |
| 132 | + " Column('id', Integer(), primary_key=True),\n", |
| 133 | + " Column('order_id', ForeignKey('orders.id')),\n", |
| 134 | + " Column('item_id', ForeignKey('items.id')),\n", |
| 135 | + " Column('quantity', Integer())\n", |
| 136 | + ")\n", |
| 137 | + "\n", |
| 138 | + "\n", |
| 139 | + "metadata.create_all(engine)" |
| 140 | + ] |
| 141 | + } |
| 142 | + ], |
| 143 | + "metadata": { |
| 144 | + "kernelspec": { |
| 145 | + "display_name": "Python 3", |
| 146 | + "language": "python", |
| 147 | + "name": "python3" |
| 148 | + }, |
| 149 | + "language_info": { |
| 150 | + "codemirror_mode": { |
| 151 | + "name": "ipython", |
| 152 | + "version": 3 |
| 153 | + }, |
| 154 | + "file_extension": ".py", |
| 155 | + "mimetype": "text/x-python", |
| 156 | + "name": "python", |
| 157 | + "nbconvert_exporter": "python", |
| 158 | + "pygments_lexer": "ipython3", |
| 159 | + "version": "3.5.2" |
| 160 | + } |
| 161 | + }, |
| 162 | + "nbformat": 4, |
| 163 | + "nbformat_minor": 2 |
| 164 | +} |
0 commit comments