I have installed the following packages -
SQLAlchemy==1.4.41
sqlalchemy-spanner==1.2.2
alembic==1.8.1
Then I created a file main.py where I am creating a SQLAlchemy engine for cloud spanner.
from sqlalchemy import create_engine, MetaData
from sqlalchemy.ext.declarative import declarative_base
engine = create_engine(
"spanner+spanner:///projects/spanner-project/instances/spanner-instance/databases/spanner-database"
)
metadata = MetaData()
Base = declarative_base(metadata=metadata)
Then in alembic.ini I have used the same url as shown above for engine.
sqlalchemy.url = spanner+spanner:///projects/spanner-project/instances/spanner-instance/databases/spanner-database
Also in env.py I have set the base's matadata as shown below -
from app.main import Base
target_metadata = Base.metadata
I also have a models.py which has just one model as shown below -
from app.main import Base
from sqlalchemy import (
Column,
Integer,
String,
)
class Users(Base):
__tablename__ = "users"
__table_args__ = {
'extend_existing': True,
}
id = Column(Integer, primary_key=True)
name = Column(String(50))
When I run the alembic command to generate migrations I see an exception of KeyError: 'spanner+spanner'.
CMD -
alembic revision --autogenerate -m "first migrations"
Exception -
Traceback (most recent call last):
File "/usr/local/bin/alembic", line 8, in <module>
sys.exit(main())
File "/usr/local/lib/python3.10/site-packages/alembic/config.py", line 590, in main
CommandLine(prog=prog).main(argv=argv)
File "/usr/local/lib/python3.10/site-packages/alembic/config.py", line 584, in main
self.run_cmd(cfg, options)
File "/usr/local/lib/python3.10/site-packages/alembic/config.py", line 561, in run_cmd
fn(
File "/usr/local/lib/python3.10/site-packages/alembic/command.py", line 229, in revision
script_directory.run_env()
File "/usr/local/lib/python3.10/site-packages/alembic/script/base.py", line 569, in run_env
util.load_python_file(self.dir, "env.py")
File "/usr/local/lib/python3.10/site-packages/alembic/util/pyfiles.py", line 94, in load_python_file
module = load_module_py(module_id, path)
File "/usr/local/lib/python3.10/site-packages/alembic/util/pyfiles.py", line 110, in load_module_py
spec.loader.exec_module(module) # type: ignore
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/code/app/alembic/env.py", line 79, in <module>
run_migrations_online()
File "/code/app/alembic/env.py", line 68, in run_migrations_online
context.configure(
File "<string>", line 8, in configure
File "/usr/local/lib/python3.10/site-packages/alembic/runtime/environment.py", line 822, in configure
self._migration_context = MigrationContext.configure(
File "/usr/local/lib/python3.10/site-packages/alembic/runtime/migration.py", line 268, in configure
return MigrationContext(dialect, connection, opts, environment_context)
File "/usr/local/lib/python3.10/site-packages/alembic/runtime/migration.py", line 196, in __init__
self.impl = ddl.DefaultImpl.get_by_dialect(dialect)(
File "/usr/local/lib/python3.10/site-packages/alembic/ddl/impl.py", line 123, in get_by_dialect
return _impls[dialect.name]
I tried to by changing the URL in alembic.ini from spanner+sapnner:///..... to spanner:///... but then I got this exception -
Traceback (most recent call last):
File "/usr/local/bin/alembic", line 8, in <module>
sys.exit(main())
File "/usr/local/lib/python3.10/site-packages/alembic/config.py", line 590, in main
CommandLine(prog=prog).main(argv=argv)
File "/usr/local/lib/python3.10/site-packages/alembic/config.py", line 584, in main
self.run_cmd(cfg, options)
File "/usr/local/lib/python3.10/site-packages/alembic/config.py", line 561, in run_cmd
fn(
File "/usr/local/lib/python3.10/site-packages/alembic/command.py", line 229, in revision
script_directory.run_env()
File "/usr/local/lib/python3.10/site-packages/alembic/script/base.py", line 569, in run_env
util.load_python_file(self.dir, "env.py")
File "/usr/local/lib/python3.10/site-packages/alembic/util/pyfiles.py", line 94, in load_python_file
module = load_module_py(module_id, path)
File "/usr/local/lib/python3.10/site-packages/alembic/util/pyfiles.py", line 110, in load_module_py
spec.loader.exec_module(module) # type: ignore
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/code/app/alembic/env.py", line 79, in <module>
run_migrations_online()
File "/code/app/alembic/env.py", line 61, in run_migrations_online
connectable = engine_from_config(
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/create.py", line 743, in engine_from_config
return create_engine(url, **options)
File "<string>", line 2, in create_engine
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/util/deprecations.py", line 309, in warned
return fn(*args, **kwargs)
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/create.py", line 522, in create_engine
entrypoint = u._get_entrypoint()
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/url.py", line 655, in _get_entrypoint
cls = registry.load(name)
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/util/langhelpers.py", line 343, in load
raise exc.NoSuchModuleError(
sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:spanner
Also when I use spanner+spanner:///... to creating and engine and then further use it to create db tables then I am able to do that successfully.
from app.main import engine, Base
from sqlalchemy import (
Column,
Integer,
String,
)
class Users(Base):
__tablename__ = "users"
__table_args__ = {
'extend_existing': True,
}
id = Column(Integer, primary_key=True)
name = Column(String(50))
Base.metadata.create_all(engine)
Just by running this model I am able to create the table.
python models.py
What do I do to use alembic with spanner?
You're using SQLAlchemy 1.4, which means it must be everywhere spanner+spanner. Just spanner will not work. As the problem appears only for Alembic, reinstall will also not help - the problem is in configurations somewhere.
Can you show your env.py file? There must be written something like this:
class SpannerImpl(DefaultImpl):
__dialect__ = "spanner+spanner"
Looking at your traceback, I suspect, in the env.py the dialect is written as just spanner, which causes the error.
Related
I'm trying to run the following code but I got a sqlalchemy invalid literal for int() with base 10: 'None' error.
import os
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from dotenv import load_dotenv
load_dotenv('.env')
#MOben DB
moben_db_creds = {
'host' : os.getenv('POSTGRES_MOBEN_HOST'),
'port' : os.getenv('POSTGRES_MOBEN_PORT'),
'user' : os.getenv('POSTGRES_MOBEN_USER'),
'password' : os.getenv('POSTGRES_MOBEN_DBPASS'),
'name' : os.getenv('POSTGRES_MOBEN_DBNAME'),
'type' : os.getenv('POSTGRES_MOBEN_DBTYPE')
}
sqlalchemy_db_uri_conn = f"{moben_db_creds['type']}://{moben_db_creds['user']}:{moben_db_creds['password']}#{moben_db_creds['host']}:{moben_db_creds['port']}/{moben_db_creds['name']}"
# DEFINE THE ENGINE (CONNECTION OBJECT)
engine = create_engine(sqlalchemy_db_uri_conn)
# CREATE A SESSION OBJECT TO INITIATE QUERY IN DATABASE
session = sessionmaker(autocommit=False, autoflush=False, bind=engine)
base = declarative_base()
def get_db():
db = session()
try:
yield db
finally:
db.close()
As i am quite new to this orm, unable to identify the solution to this error.
please follow below Traceback for more detail
Traceback: ==============================================================
(env_ucom_csb) PS C:\Users\e008329\Downloads\project_zone\ucom-csb-payload-publisher> uvicorn main:app --reload
INFO: Will watch for changes in these directories: ['C:\\Users\\e008329\\Downloads\\project_zone\\ucom-csb-payload-publisher']
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [27160] using WatchFiles
Process SpawnProcess-1:
Traceback (most recent call last):
File "C:\Users\e008329\AppData\Local\Programs\Python\Python38\lib\multiprocessing\process.py", line 315, in _bootstrap
self.run()
File "C:\Users\e008329\AppData\Local\Programs\Python\Python38\lib\multiprocessing\process.py", line 108, in run
self._target(*self._args, **self._kwargs)
File "c:\users\e008329\downloads\project_zone\env_ucom_csb\lib\site-packages\uvicorn\_subprocess.py", line 76, in subprocess_started
target(sockets=sockets)
File "c:\users\e008329\downloads\project_zone\env_ucom_csb\lib\site-packages\uvicorn\server.py", line 60, in run
return asyncio.run(self.serve(sockets=sockets))
File "C:\Users\e008329\AppData\Local\Programs\Python\Python38\lib\asyncio\runners.py", line 44, in run
return loop.run_until_complete(main)
File "C:\Users\e008329\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 616, in run_until_complete
return future.result()
File "c:\users\e008329\downloads\project_zone\env_ucom_csb\lib\site-packages\uvicorn\server.py", line 67, in serve
config.load()
File "c:\users\e008329\downloads\project_zone\env_ucom_csb\lib\site-packages\uvicorn\config.py", line 477, in load
self.loaded_app = import_from_string(self.app)
File "c:\users\e008329\downloads\project_zone\env_ucom_csb\lib\site-packages\uvicorn\importer.py", line 21, in import_from_string
module = importlib.import_module(module_str)
File "C:\Users\e008329\AppData\Local\Programs\Python\Python38\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 848, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\e008329\Downloads\project_zone\ucom-csb-payload-publisher\.\main.py", line 3, in <module>
from publish_ucom_csb import publish_uci_all
File "C:\Users\e008329\Downloads\project_zone\ucom-csb-payload-publisher\.\publish_ucom_csb.py", line 2, in <module>
from models.v_payload_ucom_csb import VPayloadUCOMCsb
File "C:\Users\e008329\Downloads\project_zone\ucom-csb-payload-publisher\.\models\v_payload_ucom_csb.py", line 2, in <module> from config.database import base
File "C:\Users\e008329\Downloads\project_zone\ucom-csb-payload-publisher\.\config\database.py", line 31, in <module>
engine = create_engine(sqlalchemy_db_uri_conn)
File "<string>", line 2, in create_engine
File "c:\users\e008329\downloads\project_zone\env_ucom_csb\lib\site-packages\sqlalchemy\util\deprecations.py", line 277, in warned
return fn(*args, **kwargs) # type: ignore[no-any-return]
File "c:\users\e008329\downloads\project_zone\env_ucom_csb\lib\site-packages\sqlalchemy\engine\create.py", line 552, in create_engine
u = _url.make_url(url)
File "c:\users\e008329\downloads\project_zone\env_ucom_csb\lib\site-packages\sqlalchemy\engine\url.py", line 838, in make_url return _parse_url(name_or_url)
File "c:\users\e008329\downloads\project_zone\env_ucom_csb\lib\site-packages\sqlalchemy\engine\url.py", line 893, in _parse_url
components["port"] = int(components["port"])
ValueError: invalid literal for int() with base 10: 'None'
You can pass a default port value when you are using the os.getenv (see https://docs.python.org/3/library/os.html#os.getenv)
e.g.
DEFAULT_PORT_VALUE = 9999
moben_db_creds = {
'host' : os.getenv('POSTGRES_MOBEN_HOST'),
'port' : os.getenv('POSTGRES_MOBEN_PORT', DEFAULT_PORT_VALUE),
...
}
Right now it seems you are passing None in the line referenced by the traceback:
components["port"] = int(components["port"])
The other option would be to use get instead of [] syntax to get the value from the components dict.
components["port"] = int(components.get("port", DEFAULT_PORT_VALUE))
Btw. it's necessarily about the env file contents it might be about its location (or the command you are using to run the script / start the server). Print or log the values after you get them from the env file and see if they are being read correctly.
sqlalchemy searches my tables twice.
I've been looking for a problem all day, but I can't find it
apps/books/models
print('before')
class Book(Base):
__tablename__ = 'books'
id = Column(Integer, primary_key=True)
title = Column(String, nullable=False,unique=True)
descriptions = Column(String)
file_path = Column(String,unique=True)
users = relationship("User", secondary="book_users", back_populates='books')
book_users = Table('book_users', Base.metadata,
Column('book_id', ForeignKey('books.id'), primary_key=True),
Column('users_id', ForeignKey('users.id'), primary_key=True),
)
print('after')
class Part(Base):
__tablename__ = 'parts'
id = Column(Integer, primary_key=True)
title = Column(String, nullable=False)
descriptions = Column(String)
book_id = Column('book_id',ForeignKey('books.id'))
book = relationship('Book',backref='parts')
apps/users/models
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True, index=True)
username = Column(String, unique=True)
last_name = Column(String)
hashed_password = Column(String)
books = relationship("Book", secondary="book_users", back_populates='users')
apps/database
from decouple import config
USER = config('user')
PASSWORD = config('password')
DB = config('db')
HOST = config('host')
SQLALCHEMY_DATABASE_URL = f"postgresql://{USER}:{PASSWORD}#{HOST}/{DB}"
engine = create_engine(SQLALCHEMY_DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()
def get_db():
db = Session(bind=engine)
try:
yield db
finally:
db.close()
im tryed without decouple,it didn't help
apps/main
import sys,os
sys.path.insert(0, os.path.abspath(".."))
sys.path.insert(0, os.path.abspath("..."))
from fastapi import FastAPI
from database import engine, Base
from users.routers import router as router_user
from books.routers import router as router_book
def create_db():
Base.metadata.create_all(bind=engine)
app = FastAPI(title="Shop")
#app.on_event("startup")
def on_startup():
create_db()
app.include_router(router=router_user)
app.include_router(router=router_book)
inserts is only one way for my imports)
exception
before
after
before
Process SpawnProcess-4:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/multiprocessing/process.py", line 315, in _bootstrap
self.run()
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/multiprocessing/process.py", line 108, in run
self._target(*self._args, **self._kwargs)
File "/Users/kana/Desktop/author/env/lib/python3.10/site-packages/uvicorn/subprocess.py", line 76, in subprocess_started
target(sockets=sockets)
File "/Users/kana/Desktop/author/env/lib/python3.10/site-packages/uvicorn/server.py", line 60, in run
return asyncio.run(self.serve(sockets=sockets))
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/runners.py", line 44, in run
return loop.run_until_complete(main)
File "uvloop/loop.pyx", line 1501, in uvloop.loop.Loop.run_until_complete
File "/Users/kana/Desktop/author/env/lib/python3.10/site-packages/uvicorn/server.py", line 67, in serve
config.load()
File "/Users/kana/Desktop/author/env/lib/python3.10/site-packages/uvicorn/config.py", line 458, in load
self.loaded_app = import_from_string(self.app)
File "/Users/kana/Desktop/author/env/lib/python3.10/site-packages/uvicorn/importer.py", line 21, in import_from_string
module = importlib.import_module(module_str)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/Users/kana/Desktop/author/apps/./main.py", line 9, in <module>
from books.routers import router as router_book
File "/Users/kana/Desktop/author/apps/./books/routers.py", line 13, in <module>
from .models import Book, Part
File "/Users/kana/Desktop/author/apps/./books/models.py", line 9, in <module>
class Book(Base):
File "/Users/kana/Desktop/author/env/lib/python3.10/site-packages/sqlalchemy/orm/decl_api.py", line 72, in __init__
_as_declarative(reg, cls, dict_)
File "/Users/kana/Desktop/author/env/lib/python3.10/site-packages/sqlalchemy/orm/decl_base.py", line 126, in _as_declarative
return _MapperConfig.setup_mapping(registry, cls, dict_, None, {})
File "/Users/kana/Desktop/author/env/lib/python3.10/site-packages/sqlalchemy/orm/decl_base.py", line 177, in setup_mapping
return cfg_cls(registry, cls_, dict_, table, mapper_kw)
File "/Users/kana/Desktop/author/env/lib/python3.10/site-packages/sqlalchemy/orm/decl_base.py", line 322, in __init__
self._setup_table(table)
File "/Users/kana/Desktop/author/env/lib/python3.10/site-packages/sqlalchemy/orm/decl_base.py", line 811, in _setup_table
table_cls(
File "<string>", line 2, in __new__
File "/Users/kana/Desktop/author/env/lib/python3.10/site-packages/sqlalchemy/util/deprecations.py", line 309, in warned
return fn(*args, **kwargs)
File "/Users/kana/Desktop/author/env/lib/python3.10/site-packages/sqlalchemy/sql/schema.py", line 584, in __new__
raise exc.InvalidRequestError(
sqlalchemy.exc.InvalidRequestError: Table 'books' is already defined for this MetaData instance. Specify 'extend_existing=True' to redefine options and columns on an existing Table object.
how you can look, my program broken in apps/books/models after
class Book(Base)
if i switch Book and Part, error will be in Part
any hint will be gratefully accepted
If you encounter this problem, you need to thoroughly change every import in your program, specifying the same path from the root directory everywhere.
another reason for the problem may be the architecture of the project.
my first reason why my database didn't create tables is because it was on the same level as the tables. in my case, it was in apps/utils/main when the tables were at the apps/books/models
level, he added it to Base.metadata.tables, but when he moved to apps/utils/main the Base.metadata.tables list was empty.
when I pulled it to the current level, the problem disappeared
resume:
string Base.metadata.create_all(bind=engine) must be a level higher than module models (or another name your models)
and update your imports
I want to add columns to a table in my database dynamically since I don't want to have to specify all columns when I set up the table in the SQLalchemy class
In order to solve this I am using alembic to add columns to the table but I am having problems.
In my python script I have defined a class as below.
DATABASE_URL_dev = "postgresql+psycopg2://user:password#localhost:5432/testdb"
engine = create_engine(DATABASE_URL_dev)
Base = declarative_base(engine)
class my_class1(Base):
__tablename__ = "test1"
id = Column(Integer, primary_key=True)
list = Column(String)
def loadsession():
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()
return session
session = loadsession()
Now I want to add a new column ("new_column") to the table test1 in the database.
Using Alembic I then followed the instructions at https://alembic.sqlalchemy.org/en/latest/tutorial.html and did the following:
Installed alembic by running pip install alembic
Created an environment using alembic init alembic
Edited the .ini file by inputting "postgresql+psycopg2://user:password#localhost:5432/testdb" at sqlalchemy.url
Created a migration script by running alembic revision -m "add a column "
I then edited the script by adding the following "op.add_column('test1', sa.Column('new_column', sa.String))" in upgrade()
Running first migration with "alembic upgrade head"
This gave me the following error:
Traceback (most recent call last):
File "c:\users\user\appdata\local\programs\python\python38-32\lib\runpy.py", line 193, in _run_module_as_main
return _run_code(code, main_globals, None,
File "c:\users\user\appdata\local\programs\python\python38-32\lib\runpy.py", line 86, in _run_code
exec(code, run_globals)
File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\Scripts\alembic.exe\__main__.py", line 9, in <module>
File "c:\users\user\appdata\local\programs\python\python38-32\lib\site-packages\alembic\config.py", line 577, in main
CommandLine(prog=prog).main(argv=argv)
File "c:\users\user\appdata\local\programs\python\python38-32\lib\site-packages\alembic\config.py", line 571, in main
self.run_cmd(cfg, options)
File "c:\users\user\appdata\local\programs\python\python38-32\lib\site-packages\alembic\config.py", line 548, in run_cmd
fn(
File "c:\users\user\appdata\local\programs\python\python38-32\lib\site-packages\alembic\command.py", line 298, in upgrade
script.run_env()
File "c:\users\user\appdata\local\programs\python\python38-32\lib\site-packages\alembic\script\base.py", line 489, in run_env
util.load_python_file(self.dir, "env.py")
File "c:\users\user\appdata\local\programs\python\python38-32\lib\site-packages\alembic\util\pyfiles.py", line 98, in load_python_file
module = load_module_py(module_id, path)
File "c:\users\user\appdata\local\programs\python\python38-32\lib\site-packages\alembic\util\compat.py", line 184, in load_module_py
spec.loader.exec_module(module)
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "y\env.py", line 77, in <module>
run_migrations_online()
File "y\env.py", line 65, in run_migrations_online
with connectable.connect() as connection:
File "c:\users\user\appdata\local\programs\python\python38-32\lib\site-packages\sqlalchemy\engine\base.py", line 2218, in connect
return self._connection_cls(self, **kwargs)
File "c:\users\user\appdata\local\programs\python\python38-32\lib\site-packages\sqlalchemy\engine\base.py", line 103, in __init__
else engine.raw_connection()
File "c:\users\user\appdata\local\programs\python\python38-32\lib\site-packages\sqlalchemy\engine\base.py", line 2317, in raw_connection
return self._wrap_pool_connect(
File "c:\users\user\appdata\local\programs\python\python38-32\lib\site-packages\sqlalchemy\engine\base.py", line 2288, in _wrap_pool_connect
Connection._handle_dbapi_exception_noconnection(
File "c:\users\user\appdata\local\programs\python\python38-32\lib\site-packages\sqlalchemy\engine\base.py", line 1554, in _handle_dbapi_exception_noconnection
util.raise_(
File "c:\users\user\appdata\local\programs\python\python38-32\lib\site-packages\sqlalchemy\util\compat.py", line 178, in raise_
raise exception
File "c:\users\user\appdata\local\programs\python\python38-32\lib\site-packages\sqlalchemy\engine\base.py", line 2285, in _wrap_pool_connect
return fn()
File "c:\users\user\appdata\local\programs\python\python38-32\lib\site-packages\sqlalchemy\pool\base.py", line 303, in unique_connection
return _ConnectionFairy._checkout(self)
File "c:\users\user\appdata\local\programs\python\python38-32\lib\site-packages\sqlalchemy\pool\base.py", line 773, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "c:\users\user\appdata\local\programs\python\python38-32\lib\site-packages\sqlalchemy\pool\base.py", line 492, in checkout
rec = pool._do_get()
File "c:\users\user\appdata\local\programs\python\python38-32\lib\site-packages\sqlalchemy\pool\impl.py", line 238, in _do_get
return self._create_connection()
File "c:\users\user\appdata\local\programs\python\python38-32\lib\site-packages\sqlalchemy\pool\base.py", line 308, in _create_connection
return _ConnectionRecord(self)
File "c:\users\user\appdata\local\programs\python\python38-32\lib\site-packages\sqlalchemy\pool\base.py", line 437, in __init__
self.__connect(first_connect_check=True)
File "c:\users\user\appdata\local\programs\python\python38-32\lib\site-packages\sqlalchemy\pool\base.py", line 657, in __connect
pool.logger.debug("Error on connect(): %s", e)
File "c:\users\user\appdata\local\programs\python\python38-32\lib\site-packages\sqlalchemy\util\langhelpers.py", line 68, in __exit__
compat.raise_(
File "c:\users\user\appdata\local\programs\python\python38-32\lib\site-packages\sqlalchemy\util\compat.py", line 178, in raise_
raise exception
File "c:\users\user\appdata\local\programs\python\python38-32\lib\site-packages\sqlalchemy\pool\base.py", line 652, in __connect
connection = pool._invoke_creator(self)
File "c:\users\user\appdata\local\programs\python\python38-32\lib\site-packages\sqlalchemy\engine\strategies.py", line 114, in connect
return dialect.connect(*cargs, **cparams)
File "c:\users\user\appdata\local\programs\python\python38-32\lib\site-packages\sqlalchemy\engine\default.py", line 488, in connect
return self.dbapi.connect(*cargs, **cparams)
File "c:\users\c\appdata\local\programs\python\python38-32\lib\site-packages\psycopg2\__init__.py", line 126, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError)
(Background on this error at: http://sqlalche.me/e/e3q8)
Questions
Is this the right way to proceed in order to add columns dynamically or should it be done in another way?
How do I resolve the sqlalchemy operationalerror?
Thanks
Try this in step 5
op.add_column('test1', sa.Column('new_column', sa.String()))
I have created a model named file. It was running well, then I created an element inside the class named set_user which gave an error. So, I removed it. But, I think it's not deleted my my database as I am getting this error while trying to migrate:
Traceback (most recent call last): File "manage.py", line 10, in
execute_from_command_line(sys.argv) File "/usr/lib/python3/dist-packages/django/core/management/init.py",
line 363, in execute_from_command_line
utility.execute() File "/usr/lib/python3/dist-packages/django/core/management/init.py",
line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv) File "/usr/lib/python3/dist-packages/django/core/management/base.py", line
283, in run_from_argv
self.execute(*args, **cmd_options) File "/usr/lib/python3/dist-packages/django/core/management/base.py", line
330, in execute
output = self.handle(*args, **options) File "/usr/lib/python3/dist-packages/django/core/management/commands/makemigrations.py",
line 96, in handle
loader = MigrationLoader(None, ignore_no_migrations=True) File "/usr/lib/python3/dist-packages/django/db/migrations/loader.py", line
52, in init
self.build_graph() File "/usr/lib/python3/dist-packages/django/db/migrations/loader.py", line
203, in build_graph
self.load_disk() File "/usr/lib/python3/dist-packages/django/db/migrations/loader.py", line
114, in load_disk
migration_module = import_module("%s.%s" % (module_name, migration_name)) File "/usr/lib/python3.6/importlib/init.py",
line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level) File "", line 994, in _gcd_import File
"", line 971, in _find_and_load File
"", line 955, in _find_and_load_unlocked
File "", line 665, in _load_unlocked
File "", line 678, in
exec_module File "", line 219, in
_call_with_frames_removed File "/home/hashir/website/one/migrations/0017_auto_20171222_2108.py", line
11, in
class Migration(migrations.Migration): File "/home/hashir/website/one/migrations/0017_auto_20171222_2108.py", line
21, in Migration
field=models.ForeignKey(default=one.models.file.set_user, on_delete=django.db.models.deletion.CASCADE,
to=settings.AUTH_USER_MODEL), AttributeError: type object 'file' has
no attribute 'set_user'
Here's models.py:
from django.db import models
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
class file(models.Model):
title = models.CharField(max_length=250)
FILE_TYPE_CHOICES = (
('audio','Audio'),
('games','Games'),
('videos','Videos'),
('applications','Applications'),
('books','Books/Docs'),
('others','Others')
)
file_type = models.CharField(max_length=12,choices=FILE_TYPE_CHOICES,default='others')
description = models.TextField(max_length=6000)
def get_absolute_url(self):
return reverse('one:user')
def __str__(self):
return self.title
Just delete migrations folder from the project and apply the migrations again.
Then run these 3 commands:
python manage.py migrate yourapp zero
python manage.py makemigrations yourapp
python manage.py migrate yourapp
Trying to run Python3.2, SQLAlchemy0.8 and MySQL5.2 on Ubuntu using Eclispse but I keep getting the error below. Am using pymysql (pymysql3 actually) engine.
module monitor
from sqlalchemy import create_engine, MetaData
from sqlalchemy.ext.declarative import declarative_base
Engine = create_engine('mysql+pymysql://user:mypass#localhost/mydb')
Base = declarative_base(Engine)
Metadata = MetaData(bind=Engine)
from sqlalchemy.orm import sessionmaker
from sqlalchemy import Table, Column, Integer
Session = sessionmaker(bind=Engine)
session = Session()
class Student(Base):
__table__ = Table('student_name', Metadata,
Column('id', Integer, primary_key=True),
autoload=True)
With that, when I run the module it throws the error as indicated below. What am I doing wrong?
Traceback (most recent call last):
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/pool.py", line 757, in _do_get
return self._pool.get(wait, self._timeout)
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/util/queue.py", line 166, in get
raise Empty
sqlalchemy.util.queue.Empty
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/lukik/workspace/upark/src/monitor.py", line 12, in <module>
class Parking(Base):
File "/home/lukik/workspace/upark/src/monitor.py", line 15, in Parking
autoload=True)
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/schema.py", line 333, in __new__
table._init(name, metadata, *args, **kw)
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/schema.py", line 397, in _init
self._autoload(metadata, autoload_with, include_columns)
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/schema.py", line 425, in _autoload
self, include_columns, exclude_columns
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/engine/base.py", line 1604, in run_callable
with self.contextual_connect() as conn:
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/engine/base.py", line 1671, in contextual_connect
self.pool.connect(),
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/pool.py", line 272, in connect
return _ConnectionFairy(self).checkout()
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/pool.py", line 425, in __init__
rec = self._connection_record = pool._do_get()
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/pool.py", line 777, in _do_get
con = self._create_connection()
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/pool.py", line 225, in _create_connection
return _ConnectionRecord(self)
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/pool.py", line 322, in __init__
exec_once(self.connection, self)
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/event.py", line 381, in exec_once
self(*args, **kw)
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/event.py", line 398, in __call__
fn(*args, **kw)
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/engine/strategies.py", line 168, in first_connect
dialect.initialize(c)
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/dialects/mysql/base.py", line 2052, in initialize
default.DefaultDialect.initialize(self, connection)
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/engine/default.py", line 172, in initialize
self._get_default_schema_name(connection)
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/dialects/mysql/base.py", line 2019, in _get_default_schema_name
return connection.execute('SELECT DATABASE()').scalar()
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/engine/base.py", line 664, in execute
params)
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/engine/base.py", line 808, in _execute_text
statement, parameters
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/engine/base.py", line 871, in _execute_context
context)
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/engine/default.py", line 322, in do_execute
cursor.execute(statement, parameters)
File "/usr/local/lib/python3.2/dist-packages/pymysql/cursors.py", line 105, in execute
query = query % escaped_args
TypeError: unsupported operand type(s) for %: 'bytes' and 'tuple'
mysql-connector-python and oursql work fine for me under py3k.
How to install?
$ pip install mysql-connector-python
Usage
Engine = create_engine('mysql+mysqlconnector://<USERNAME>:<PASSWD>#<HOSTNAME>:<PORT>/<DBNAME>')
Stay on py2k or a different DB driver for the time being. This is a known SQLAlchemy bug: 2663.