127 lines
3.3 KiB
Text
127 lines
3.3 KiB
Text
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"id": "initial_id",
|
|
"metadata": {
|
|
"collapsed": true,
|
|
"ExecuteTime": {
|
|
"end_time": "2024-05-22T02:36:26.729901Z",
|
|
"start_time": "2024-05-22T02:36:26.638074Z"
|
|
}
|
|
},
|
|
"source": [
|
|
"from sqlalchemy import Boolean, Column, ForeignKey, Integer, Text, create_engine, select\n",
|
|
"from sqlalchemy.orm import DeclarativeBase, relationship, Session\n",
|
|
"from sqlalchemy.orm import sessionmaker\n",
|
|
"from sqlalchemy.orm.relationships import Relationship"
|
|
],
|
|
"execution_count": 1,
|
|
"outputs": []
|
|
},
|
|
{
|
|
"metadata": {
|
|
"ExecuteTime": {
|
|
"end_time": "2024-05-22T02:36:26.750099Z",
|
|
"start_time": "2024-05-22T02:36:26.730894Z"
|
|
}
|
|
},
|
|
"cell_type": "code",
|
|
"source": [
|
|
"POSTGRES_CONNECTION_STRING = \"postgresql://cf_developer:XJcbU37MBYeh4dDK6PTV5n@sqlalchemy-experiments.postgres.database.azure.com:5432/postgres\"\n",
|
|
"\n",
|
|
"class Base(DeclarativeBase):\n",
|
|
" pass\n",
|
|
"\n",
|
|
"\n",
|
|
"class Author(Base):\n",
|
|
" __tablename__: str = \"authors\"\n",
|
|
"\n",
|
|
" id: Column[int] = Column(Integer, primary_key=True)\n",
|
|
" name: Column[str] = Column(Text, nullable=False)\n",
|
|
"\n",
|
|
"\n",
|
|
"class Book(Base):\n",
|
|
" __tablename__: str = \"books\"\n",
|
|
"\n",
|
|
" id: Column[int] = Column(Integer, primary_key=True)\n",
|
|
" title: Column[str] = Column(Text, nullable=False)\n",
|
|
" author_id: Column[int] = Column(Integer, ForeignKey(\"authors.id\"), nullable=False)\n",
|
|
" is_bestseller: Column[bool] = Column(Boolean, default=False)\n",
|
|
"\n",
|
|
" author: Relationship[Author] = relationship(\"Author\", backref=\"books\")\n"
|
|
],
|
|
"id": "f9ddc0d6d98a89e2",
|
|
"execution_count": 2,
|
|
"outputs": []
|
|
},
|
|
{
|
|
"metadata": {
|
|
"ExecuteTime": {
|
|
"end_time": "2024-05-22T02:36:26.773835Z",
|
|
"start_time": "2024-05-22T02:36:26.750664Z"
|
|
}
|
|
},
|
|
"cell_type": "code",
|
|
"source": "engine = create_engine(POSTGRES_CONNECTION_STRING, echo=True)",
|
|
"id": "a52819969951369c",
|
|
"execution_count": 3,
|
|
"outputs": []
|
|
},
|
|
{
|
|
"metadata": {
|
|
"ExecuteTime": {
|
|
"end_time": "2024-05-22T02:36:26.777315Z",
|
|
"start_time": "2024-05-22T02:36:26.774957Z"
|
|
}
|
|
},
|
|
"cell_type": "code",
|
|
"source": [
|
|
"\n",
|
|
"\n",
|
|
"session_factory = sessionmaker(bind=engine)\n",
|
|
"session: Session = session_factory()\n",
|
|
"stmt = select(Book).where(Book.title.in_([\"book0\", \"book1\"]))\n",
|
|
"\n",
|
|
"#print(session.scalars(stmt).one())\n"
|
|
],
|
|
"id": "78a6b79f917779e",
|
|
"execution_count": 4,
|
|
"outputs": []
|
|
},
|
|
{
|
|
"metadata": {
|
|
"ExecuteTime": {
|
|
"end_time": "2024-05-22T02:36:26.779539Z",
|
|
"start_time": "2024-05-22T02:36:26.778077Z"
|
|
}
|
|
},
|
|
"cell_type": "code",
|
|
"source": "session.scalars(stmt).one()",
|
|
"id": "97cfebcd26c9995f",
|
|
"execution_count": 5,
|
|
"outputs": []
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 2
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython2",
|
|
"version": "2.7.6"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|