SQLAlchmemy — get related objects with reflected tables - sqlalchemy

I am quite new to sqlalchemy, I guess I am missing just a little piece here.
There is this Database (sql):
create table CEO (
id int not null auto_increment,
name char(255) not null,
primary key(id),
unique(name)
);
create table Company (
id int not null auto_increment,
name char (255) not null,
ceo int not null,
primary key(id),
foreign key(ceo) references CEO(id)
);
That code:
from sqlalchemy import create_engine, Table, Column, Integer, String, ForeignKey
from sqlalchemy.orm import registry, relationship, Session
engine = create_engine(
"mysql+pymysql:xxxxxxxx",
echo=True,
future=True
)
mapper_registry = registry()
Base = mapper_registry.generate_base()
#####################
## MAPPING CLASSES ##
#####################
class CEO(Base):
__table__ = Table('CEO', mapper_registry.metadata, autoload_with=engine)
companies = relationship('Company', lazy="joined")
class Company(Base):
__table__ = Table('Company', mapper_registry.metadata, autoload_with=engine)
##########################
## FINALLY THE QUESTION ##
##########################
with Session(engine, future=True) as session:
for row in session.query(CEO).all():
for company in row.companies:
## Just the id of the Ceo is yielded here
print(company.ceo)
So CEO.companies works as expected, but Company.ceo does not, even though the FOREIGN KEY is defined.
What is a proper setup for the Company Mapper class, such that Company.ceo yields the related object?

I could figure out, that the automatic setup did not work, because the column Company.ceo exists in the Database and represents the ID of a given row. To make everything work, I needed to rename Company.ceo to Company.ceo_id and add the relation manually like so:
CompanyTable = Table('Company', Base.metadata, autoload_with=engine)
class Company(Base):
__table__ = CompanyTable
ceo_id = CompanyTable.c.ceo
ceo = relationship('CEO')
I would like to know if it would be possible to rename the column within the Table(…) call, such that I could get rid of the extra CompanyTable thing.

Related

Parameter index out of range with Multiple Join and Optional input parameter

I have an issue with the multiple Join using Spring JdbcTemplate if I need to make optional the input parameters.
This is the scenario.
The SQL table where i must perform the Join are:
CREATE TABLE users (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(15),
password VARCHAR(20),
info VARCHAR(30),
active BOOLEAN,
locked BOOLEAN,
lockout_duration INT DEFAULT 0,
lockout_limit DATETIME,
login_attempts INT DEFAULT 0,
PRIMARY KEY(id)
);
CREATE TABLE profiles (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(15),
info VARCHAR(30),
PRIMARY KEY(id)
);
CREATE TABLE profiling(
user_id INT NOT NULL,
profile_id INT NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (profile_id) REFERENCES profiles(id) ON DELETE CASCADE,
PRIMARY KEY(user_id,profile_id)
);
Where profiling is the table to associate a user with his profile; the profile must be intended as the identification of what actions are permitted to users.
In one my precedent post, i ask how to make optional the sql parameters and I obtained a perfect response, that works and i have always used since then. So, if i need to make this, is put in the where:
WHERE (is null or variabile_name = ?)
And, using jdbctemplate, i write:
jdbcTemplate.query(SQL, new Object[]{variabile_name, variabile_name}, mapper_name);
where, of course, SQL is the String object where i make the Query.
So, i make this also in this case, but i got the error:
java.lang.IllegalStateException: Failed to execute CommandLineRunner
Caused by: java.sql.SQLException: Parameter index out of range (3 > number of parameters, which is 2).
I report the full method here:
/**
* This metod return the join between users and profiles
* made using the profiling table
*
* #param userID the user id code
* #param profilesID the profile id code
* #return the join list
*/
public List<Profiling> joinWithUsersandProfiles(Integer userID, Integer profileID)
{
//This is the mapper for Profiling
RowMapper<Profiling> profilingMapper = new RowMapper<Profiling>()
{
//This method must be implemented when we use a row mapper
public Profiling mapRow(ResultSet rs, int rowNum) throws SQLException
{
Profiling profiling = new Profiling();
profiling.setUser_id(rs.getInt("profiling.user_id"));
profiling.setProfile_id(rs.getInt("profiling.profile_id"));
//mapping users variabiles
profiling.setUsersId(rs.getInt("users.id"));
profiling.setUsersActive(rs.getBoolean("users.active"));
profiling.setUsersInfo(rs.getString("users.info"));
profiling.setUsersLocked(rs.getBoolean("users.locked"));
profiling.setUsersLockoutDuration(rs.getInt("users.lockout_duration"));
profiling.setUsersLockoutLimit(rs.getTime("users.lockout_limit"));
profiling.setUsersLoginAttempts(rs.getInt("users.login_attempts"));
profiling.setUsersName(rs.getString("users.name"));
profiling.setUsersPassword(rs.getString("users.password"));
//mapping profiles variabiles
profiling.setProfilesId(rs.getInt("profiles.id"));
profiling.setProfilesInfo(rs.getString("profiles.info"));
profiling.setProfilesName(rs.getString("profiles.name"));
return profiling;
}
};
/**
* This is the string that contain the query to obtain the data from profiling table.
* Please note that the notation "? is null or x = ?" means that the x variabile is optional;
* it can be asked as no making the query.
* If we give alla input equals null, it means that we must perform a SQl Select * From table.
*/
String SQL = "SELECT * FROM profiling JOIN users ON profiling.user_id = users.id JOIN profiles ON profiling.profile_id = profiles.id WHERE (is null or profiling.user_id = ?) AND (is null or profiling.profile_id = ?)";
/**
* The list containing the results is obtained using the method query on jdcbtemplate, giving in in input to it the query string, the array of object
* containing the input variabile of the method and the rowmapper implemented.
*/
List<Profiling> theProfilings = jdbcTemplate.query(SQL, new Object[]{userID, userID, profileID, profileID}, profilingMapper);
return theProfilings;
}
I know that the problem is made by the optional variabile. Why? if i try to remove the optional code and pass from:
(is null or variabile_name = ?)
to
variabile_name = ?
The code work perfectly.
So, what's my error here?
Edit: solved myself. I forgot the ? BEFORE the "is null" code. So, passing to:
(? is null or variabile_name = ?)
The code works.

How to return a single JSON object from multiple tables with PostgreSQL

With the following snippet I am building a valid JSON string:
(SELECT CONCAT('{', array_to_string(array_agg(info),','), '}')
FROM (
SELECT CONCAT('"', "displayOrder", '":',
CONCAT('{"milestoneID":',
"milestoneID"::TEXT,
',"msValue":"',
"msValue",
'","msColor":"',
"msColor", '"}'
)
) AS info
FROM "fileMilestones"
LEFT JOIN milestones ON "fileMilestones"."milestoneID" = milestones.id
WHERE "fileMilestones"."fileNumber" = wc_files."fileNumber"
) AS msa
) AS ms,
However, it is just a string and is not seen as a JSON object. I am sure I am doing this wrong...
Here is the table structure:
CREATE TABLE public."fileMilestones"
(
id integer NOT NULL DEFAULT nextval('"fileMilestones_id_seq"'::regclass),
"fileNumber" integer,
"milestoneID" smallint,
"msValue" text,
"msColor" text,
CONSTRAINT "fileMilestones-id.constraint" PRIMARY KEY (id),
CONSTRAINT "fileMilestones-fileNumber-milestoneID.constraint" UNIQUE ("fileNumber", "milestoneID")
)
CREATE TABLE public.milestones
(
id integer NOT NULL DEFAULT nextval('milestones_id_seq'::regclass),
"displayOrder" smallint,
name citext,
days smallint,
start smallint,
"calendarDays" smallint,
description citext,
"moduleID" integer,
CONSTRAINT "milestones-id.constraint" PRIMARY KEY (id),
CONSTRAINT "milestones-name.constraint" UNIQUE (name)
)
This is what the returned JSON needs to look like:
{"10":{"milestoneID":1, "msValue":"", "msColor":"milestoneColorGreen"},
"20":{"milestoneID":2, "msValue":"", "msColor":"milestoneColorGreen"},
"30":{"milestoneID":3, "msValue":"", "msColor":"milestoneColorGreen"},
"40":{"milestoneID":4, "msValue":"", "msColor":"milestoneColorGreen"},
"50":{"milestoneID":10, "msValue":"", "msColor":"milestoneColorGreen"},
"60":{"milestoneID":6, "msValue":"", "msColor":"milestoneColorGreen"},
"70":{"milestoneID":7, "msValue":"", "msColor":"milestoneColorGreen"},
"80":{"milestoneID":8, "msValue":"76", "msColor":""},
"90":{"milestoneID":9, "msValue":"", "msColor":"milestoneColorGreen"}}
How can I build a JSON object and return it as JSON?
The simplest solution would be to cast the final result to JSON using ::json, but you really want to use the built-in JSON support for these kinds of things. Anything above the most trivial JSON structure will quickly become very tedious to specify in code, prone to error and a nightmare to maintain.
But to answer your question (at the bottom of your post), you can "build a JSON object" with... json_build_object(). You can aggregate key/value pairs with the json_object_agg() function. Both functions also have a jsonb variant.
SELECT json_object_agg("displayOrder"::text,
json_build_object('milestoneID', "milestoneID"::text,
'msValue', "msValue",
'msColor', "msColor")) AS info
FROM "fileMilestones"
LEFT JOIN milestones ON "fileMilestones"."milestoneID" = milestones.id
WHERE "fileMilestones"."fileNumber" = wc_files."fileNumber" -- wc_files???
...

SQLAlchemy - Auto Lookup Foreign Key Relationship for Insert

I am trying to get an SQLAlchemy ORM class to automatically:
either lookup the foreign key id for a field
OR
for entries where the field isn't yet in foreign key table, add the row to the foreign key table - and use the auto generated id in the original table.
To illustrate:
Class Definition
class EquityDB_Base(object):
#declared_attr
def __tablename__(cls):
return cls.__name__.lower()
__table_args__ = {'mysql_engine': 'InnoDB'}
__mapper_args__= {'always_refresh': True}
id = Column(Integer, primary_key=True)
def fk(tablename, nullable=False):
return Column("%s_id" % tablename, Integer,
ForeignKey("%s.id" % tablename),
nullable=nullable)
class Sector(EquityDB_Base, Base):
name = Column(String(40))
class Industry(EquityDB_Base, Base):
name = Column(String(40))
sector_id = fk('sector')
sector = relationship('Sector', backref='industries')
class Equity(EquityDB_Base, Base):
symbol = Column(String(10), primary_key=True)
name = Column(String(40))
industry_id = fk('industry')
industry = relationship('Industry', backref='industries')
Using the Class to Set Industry and Sector
for i in industry_record[]:
industry = Industry(id=i.id,
name=i.name,
sector=Sector(name=i.sector_name))
session.merge(industry)
Result
Unfortunately, when I run this - the database adds individual rows to the sector table for each duplicate use of 'sector_name' - for instance, if 10 industries use 'Technology' as their sector name, I get 10 unique sector_id for each one of the 10 industries.
What I WANT - is for each time a sector name is presented that is already in the database, for it to auto-resolve to the appropriate sector_id
I am clearly just learning SQLAlchemy, but can't seem to figure out how to enable this behavior.
Any help would be appreciated!
See answer to a similar question create_or_get entry in a table.
Applying the same logic, you would have something like this:
def create_or_get_sector(sector_name):
obj = session.query(Sector).filter(Sector.name == sector_name).first()
if not obj:
obj = Sector(name = sector_name)
session.add(obj)
return obj
and use it like below:
for i in industry_record[:]:
industry = Industry(id=i.id,
name=i.name,
sector=create_or_get_sector(sector_name=i.sector_name))
session.merge(industry)
One thing you should be careful about is which session instance is used there in the create_or_get_sector.

During synchronization Foreign key null (error)

In my database I want to synchronize two tables. I use auth_user(Default table provided by Django) table for registration and there was another table user-profile that contain entities username, email, age etc. During the synchronization how to update Foriegn key?
def get_filename(instance,filename):
return "upload_files/%s_%s" % (str(time()).replace('.','_'),filename)
def create_profile(sender, **kwargs):
if kwargs["created"]:
p = profile(username = kwargs["instance"], email=kwargs["instance"])
p.save()
models.signals.post_save.connect(create_profile, sender=User)
class profile(models.Model):
username = models.CharField(max_length = 30)
email = models.EmailField()
age = models.PositiveIntegerField(default='15')
picture = models.FileField(upload_to='get_filename')
auth_user_id = models.ForeignKey(User)
Here in table profile during synchronization all columns are filled except auth_user_id. and there was an error
Exception Value:
(1048, "Column 'auth_user_id_id' cannot be null")
You have to alter your table and change the column auth_user_id_id datatype attribute that allows null.
Something like this:-
ALTER TABLE mytable MODIFY auth_user_id_id int;
Assuming auth_user_id_id as int datatype.(Columns are nullable by default)

How do I update a SQL row given an ID with LINQ to SQL?

Given this schema:
Fruits
- FruitID INT PK
- FruitName NVARCHAR(30)
- FruitStatusID INT NULL FK: Statuses
Statuses
- StatusID INT PK
- StatusName NVARCHAR(30)
If I have a FruitID in hand (just an int, not a Fruit object), how do I update the FruitName and null out FruitStatusID without loading the Fruit object from the database first?
Note: this solution gets me pretty far, but I can't figure out how to null out a FK column.
Answers in C# or VB, thanks!
This works but seems unnecessarily complicated:
''//initialize the values I'm going to null out to something
Dim Tag As Data_Tag = New Data_Tag() With {
.Data_Tag_ID = DataTagID,
.Last_Error_DateTime = New DateTime(),
.Last_Error_Message = "",
.Last_Error_Severity_Type_ID = -1 }
''//start change tracking
DB.Data_Tags.Attach(Tag)
''//record changes to these properties (must be initialized above)
Tag.Last_Error_DateTime = Nothing
Tag.Last_Error_Message = Nothing
Tag.Last_Error_Severity_Type_ID = Nothing
DB.SubmitChanges()
Surely there's a better way!
(note: the weird comment syntax is solely for the code highliger--it doesn't like VB-style comments)