session.add() and session methods does not exist - python

I'm using SqlAlchemy 1.3.20 and python3.8
In below code i used class sessionmaker.than i created an object.
when i call object methods such as : add() , commit() , query() and etc
i see this error TypeError: "Session' object is not callable.
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.orm import Sessionmaker,relationship
base = declarative_base()
engine = create_engine('sqlite:///c:\\users\\alierza\\desktop\\python\\instagram\\test.db',echo=True)
class Cars(base):
__tablename__ = 'cars'
id = Column(Integer, primary_key=True)
make = Column(String(50), nullable=False)
color = Column(String(50), nullable=False)
class carOweners(base):
__tablename__ = 'carowner'
id = Column(Integer, primary_key=True)
name = Column(String(50), nullable=False)
age = Column(Integer, nullable=False)
carid = Column(Integer, ForeignKey('cars.id'))
car = relationship('Cars')
Session=sessionmaker(bind=engine)
session=Session()
base.metadata.create_all(engine)
car1=Cars(make='Ford',color='siliver')
owenr1=carOweners(name='Joe',age=20,carid=(car1.id))
session().add(car1)
session().add(owenr1)
session().commit()
its seems that methods does not exist.
Any tips or help?

Related

Filter on many-many relationship of parent with sqlalchemy

Suppose I have the following three classes describing a music collection:
from sqlalchemy import Column, Integer, String, ForeignKey, UniqueConstraint
from sqlalchemy import Table
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
TRACK_NAME_LEN = 64
ALBUM_NAME_LEN = 64
TAG_NAME_LEN = 64
URL_LEN = 255
Base = declarative_base()
class Track(Base):
__tablename__ = 'tracks'
id = Column(Integer, primary_key=True)
name = Column(String(TRACK_NAME_LEN), nullable=False)
# many -> one
album_id = Column(Integer, ForeignKey('albums.id'), nullable=True)
album = relationship('Album', back_populates='tracks')
# Auxiliary table for many <--> many relationship
album_tag = Table('album_tag', Base.metadata,
Column('album_id', Integer, ForeignKey('albums.id')),
Column('tag_id', Integer, ForeignKey('tags.id')))
class Album(Base):
__tablename__ = 'albums'
id = Column(Integer, primary_key=True)
name = Column(String(ALBUM_NAME_LEN), nullable=False)
# one -> many
tracks = relationship('Track', back_populates='album')
# many -> many
tags = relationship(
'Tag',
secondary=album_tag,
back_populates='albums')
class Tag(Base):
__tablename__ = 'tags'
id = Column(Integer, primary_key=True)
name = Column(String(TAG_NAME_LEN), nullable=False, unique=True)
# many -> many
albums = relationship(
'Album',
secondary=album_tag,
back_populates='tags')
An album may have many different tags.
I'd like a query returning all tracks whose album has a given tag.
Here's something that doesn't work:
rock = session.query(Tag).filter(name='rock').one()
session.query(Track).join(Album).filter(Album.tags.any(rock))
There are any number of other failed attempts.
How do we achieve this?
This works:
session.query(Track).filter(Track.album.has(Album.tags.any(name='tango'))).all()

Error on join condition with SqlAlchemy

I'm trying to use SQLAlchemy on my python app but I have a problem with the many to many relationship.
I have 4 tables:
users, flags, commandes, channels, and commandes_channels_flags
commandes_channels_flags contain a foreign key for each concerned table (commandes, channels and flags)
An user has a flag_id as foreign key too.
So I try to link commandes, channels and flag. the objective is to know that a command can run on a channel for a flag.
I did this:
from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
Base = declarative_base()
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
pseudo = Column(String(50), unique=True, nullable=False)
flag_id = Column(ForeignKey('flags.id'))
class Flag(Base):
__tablename__ = 'flags'
id = Column(Integer, primary_key=True)
irc_flag = Column(Integer)
nom = Column(String(50))
users = relationship("User", backref="flag", order_by="Flag.irc_flag")
commande = relationship("Commande", secondary="commandes_channels_flags", back_populates="flags")
channel = relationship("Channel", secondary="commandes_channels_flags", back_populates="flags")
class Channel(Base):
__tablename__ = 'channels'
id = Column(Integer, primary_key=True)
uri = Column(String(50))
topic = Column(String(255))
commande = relationship("Commande", secondary="commandes_channels_flags", back_populates="channels")
flag = relationship("Flag", secondary="commandes_channels_flags", back_populates="channels")
class Commande(Base):
__tablename__ = 'commandes'
id = Column(Integer, primary_key=True)
pattern = Column(String(50))
channel = relationship("Channel", secondary="commandes_channels_flags", back_populates="commandes")
flag = relationship("Flag", secondary="commandes_channels_flags", back_populates="commandes")
class CommandeChannelFlag(Base):
__tablename__ = 'commandes_channels_flags'
id = Column(Integer, primary_key=True)
commande_id = Column(ForeignKey('commandes.id'))
channel_id = Column(ForeignKey('channels.id'))
flag_id = Column(ForeignKey('flags.id'))
But I have this error:
sqlalchemy.exc.InvalidRequestError: Mapper 'Mapper|Commande|commandes' has no property 'channels'
I understand that I have an error in my tables linking but I can't find it.
back_populates needs to match the exact name of the related property on the other model. In Channel, you have back_populates="channels", but in Commande, you have:
channel = relationship("Channel", secondary="commandes_channels_flags", back_populates="commandes")
Instead, change channel = relationship to channels = relationship.
You'll also need to change the other relationship properties to Flag.commandes, Flag.channels, Channel.commandes, Channel.flags, and Commande.flags to match your back_populates arguments.

Sqlalchemy association proxy and no_autoflush

I'm trying to figure out why I need to use a no_autoflush block when inserting data into an association proxy if the association proxy data has been accessed first. An example of this is bellow (using MySQL):
from sqlalchemy import create_engine, Integer, Column, String, ForeignKey, UniqueConstraint
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship, scoped_session
Base = declarative_base()
engine = create_engine('{}://{}:{}#{}/{}'.format(...))
session_factory = sessionmaker(bind=engine)
Session = scoped_session(session_factory)
class DomainModel(Base):
__tablename__ = 'domains'
id = Column(Integer, primary_key=True)
name = Column(String(255), nullable=False, unique=True)
domains_to_servers = relationship("DomainServerModel", back_populates="domain")
servers = association_proxy('domains_to_servers', 'server',
creator=lambda s: DomainServerModel(server=s))
class ServerModel(Base):
__tablename__ = 'servers'
id = Column(Integer, primary_key=True)
name = Column(String(128), nullable=False, unique=True, index=True)
domains_to_servers = relationship("DomainServerModel", back_populates="server")
domains = association_proxy('domains_to_servers', 'domain',
creator=lambda d: DomainServerModel(domain=d))
class DomainServerModel(Base):
__tablename__ = 'domains_to_servers'
id = Column(Integer, primary_key=True)
domain_id = Column(Integer, ForeignKey('domains.id'), nullable=False)
server_id = Column(Integer, ForeignKey('servers.id'), nullable=False)
server = relationship('ServerModel', back_populates="domains_to_servers")
domain = relationship('DomainModel', back_populates="domains_to_servers")
def test():
session = Session()
with session.no_autoflush:
s = session.query(ServerModel).filter_by(name='test.com').one()
print(s.domains)
d = DomainModel(name='test1.com')
session.add(d)
session.commit()
s.domains.append(d)
session.commit()
if __name__ == '__main__':
Base.metadata.drop_all(engine)
Base.metadata.create_all(engine)
session = Session()
session.add(ServerModel(name='test.com'))
session.commit()
test()
I'm trying to add a new domain_to_server mapping via the server/domain association proxy. If I don't access the association proxy first, ie remove the print statement in test(), then I can add the domain without needing the session.no_autoflush block. But with the print statement in there, it will fail without the session.no_autoflush block with an IntegrityError, saying that server_id cannot be null in the domains to servers table.
I'm trying to figure out why the no_autoflush block is needed here. I don't see any mention of it in the association_proxy docs. Is this simply the way it is, and all inserts into an association_proxy should to happen in a no_autoflush bock in case it has been accessed prior to the insert?

Alembic downgrade doesn't seem to understand the meta data

The model.py looks like this:
import datetime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, Numeric, ForeignKey, DateTime, Boolean
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, relationship
from configs import config_base as config
Base = declarative_base()
class User(Base):
__tablename__ = 'user'
id = Column(String, unique=True, primary_key=True)
name = Column(String(100), nullable=False)
team_id = Column(String, ForeignKey('team.id'))
last_modified_on = Column(DateTime, default=datetime.datetime.utcnow())
team = relationship('Team', back_populates='members')
class Team(Base):
__tablename__ = 'team'
id = Column(String, unique=True, primary_key=True)
name = Column(String, nullable=False)
bot_access_token = Column(String(100), nullable=False)
bot_user_id = Column(String(100), nullable=False)
last_modified_on = Column(DateTime, default=datetime.datetime.utcnow())
is_active = Column(Boolean, default=True)
members = relationship('User', back_populates='team')
is_first_time_news = Column(Boolean, default=True)
engine = create_engine(config.SQLALCHEMY_DATABASE_URI)
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
I just added is_first_time_news via this alembic migration:
revision = '6f9e2d360276'
down_revision = None
branch_labels = None
depends_on = None
from alembic import op
import sqlalchemy as sa
def upgrade():
op.add_column('team', sa.Column('is_first_time_news', sa.Boolean, default=False))
def downgrade():
op.drop_column('team', sa.Column('is_first_time_news', sa.Boolean))
alembic upgrade head works great.
But when I do a alembic downgrade -1 I get a strange exception:
AttributeError: Neither 'Column' object nor 'Comparator' object has an
attribute '_columns'
Are you using sqlite? Sqlite does not allow you to drop a column from the
scheme. I had a similar problem when I tried to downgrade a local sqlite database I was testing.
SQLite supports a limited subset of ALTER TABLE. The ALTER TABLE
command in SQLite allows the user to rename a table or to add a new
column to an existing table.
https://www.sqlite.org/lang_altertable.html
Try:
def downgrade():
op.drop_column('team', 'is_first_time_news')

SQLAlchemy import tables with relationships

I have problem with separating tables with relationships in different files. I want the tables below to be in three separate files and to import TableA in third party page, but I can not manage the load order.
In most of the time I'm receiving the following error.
sqlalchemy.exc. InvalidRequestError: When initializing mapper Mapper|TableA|tablea, expression 'TableB' failed to locate a name ("name 'TableB' is not defined"). If this is a class
name, consider adding this relationship() to the class after both dependent classes have been defined.
class TableA(Base):
__tablename__ = "tablea"
id = Column(Integer, primary_key=True)
name = Column(String)
tableB = relationship("TableB", secondary = TableC.__table__)
class TableB(Base):
__tablename__ = "tableb"
id = Column(Integer, primary_key=True)
name = Column(String)
class TableC(Base):
__tablename__ = "tableab"
tableAId = Column("table_a_id", Integer, ForeignKey("TableA.id"), primary_key=True)
tableBId = Column("table_b_id", Integer, ForeignKey("TableB.id"), primary_key=True)
This should work (note that the TableC.table is replaced with the name of the table to avoid circular module loading):
### base.py
engine = create_engine('sqlite:///:memory:', echo=True)
Session = sessionmaker(bind=engine)
Base = declarative_base(bind=engine)
### classA.py
from base import Base
from classB import TableB
class TableA(Base):
__tablename__ = 'tablea'
id = Column(Integer, primary_key=True)
name = Column(String(50))
tableBs = relationship("TableB", secondary="tableab")
#tableBs = relationship("TableB", secondary=TableC.__table__)
### classB.py
from base import Base
class TableB(Base):
__tablename__ = 'tableb'
id = Column(Integer, primary_key=True)
name = Column(String(50))
### classC.py
from base import Base
from classA import TableA
from classB import TableB
class TableC(Base):
__tablename__ = 'tableac'
tableAId = Column(Integer, ForeignKey("tablea.id"), primary_key=True, )
tableBId = Column(Integer, ForeignKey("tableb.id"), primary_key=True, )
### main.py
from base import Base, Session, engine
from classA import TableA
from classB import TableB
from classC import TableC
Base.metadata.create_all(engine)
Also I believe that the ForeignKey parameter is case sensitive, so you code might not work because "TableA.id" doe snot match "tablea" name when case-sensitive.
from sqlalchemy import Column, String, Integer
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class Parent(Base):
__tablename__ = 'Parent'
ParentID = Column(Integer, primary_key=True)
Description = Column(String)
def __init__(self, ParentID, Description):
self.ParentID = ParentID
self.Description = Description
----------------------------------------------------------------------
from sqlalchemy import Column, String, Integer, ForeignKey
import Parent
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class Child(Base):
__tablename__ = "Child"
ChildID = Column(Integer, primary_key=True)
Description = Column(String)
ParentID = Column('CompanyID', Integer, ForeignKey(Parent.ParentID))
def __init__(self, ChildID, Description,ParentID):
self.ChildID = ChildID
self.Description = Description
self.ParentID=ParentID

Categories