ActiveJDBC Many2Many with child table in different schema from parent and join tables - mysql

Say I have the one db schema named "action" that has the tables "actions" and "actionnotifications". Then I have another db schema named "notification" that has the table "notifications".
I am using ActiveJDBC to query the complex action object.
The Action class (which extends Model) has the following annotation:
#Many2Many(other = Notification.class, join = "actionnotifications", sourceFKName = "actionId", targetFKName = "notificationId")
When I run the following code:
List actions= Action.where("id = ?", actionId).include(Notification.class)
I get a MySQLSyntaxError exception saying that "Table 'action.notifications' doesn't exist". I have not seen anything in the ActiveJDBC documentation that talks about querying class relationships over different db schemas, does anyone have any experience accomplishing this task?

This is more of an issue of MySQL and not ActiveJDBC. Are you sure you can access tables from one schema while connected to another? The schema is really part of the MySQL JDBC URL, correct? If you can write a Java program that can access all your tables from different MySQL databases, that ActiveJDBC will be able to do that too.

Related

Data Associations in Node using MYSQL

When creating models in NODE using MYSQL, how does one write the relationships between Users and Products for example. How do you show there is an association. Do you not need to show association when writing the Models and just use Join in the SQL query?

Accessing ActiveRecord models from MongoDB application

I have an old app built in Rails 3 using MongoDB for the database (Mongoid as the adapter).
I am moving it to Rails 4 and using MySQL. My plan is to map the collections into their own tables through a script and copy the data. I'm comfortable doing that part. What I cannot for the life of me figure out is how to connect to the MySQL DB.
I have created the new app in Rails 4 and the new DB and, as an example, a table for Contacts which maps to the collection in the MongoDB. What I want to do is find all contacts in Mongo, then connect to the MySQL DB and insert the records into the table.
How do I do that last part?
Thanks
Robin
I managed to do this as follows.
1) Created the db schema in the new application. At this stage I didn't worry about associations as I would be setting association ids manually.
2) In a file I called transition.rb in the old app, I defined migration classes for each of the classes I wanted to transfer e.g. for the class Product, I defined a transition class called NewProduct using the following pattern:
class NewProduct < ActiveRecord::Base
file = File.open("#{Rails.root}" + "/config/database.yml")
dbconfig = YAML::load(file)
establish_connection(dbconfig["development"])
self.table_name = "products"
end
By adding the mysql2 gem to the old MongoDB application, I was able to use this pattern to connect to the relevant tables in the new application.
3) For each of the classes, I then iterated over the existing records, mapping them to the new tables through the migration classes. There are some challenges where MySQL doesn't have the columns available to map directly e.g. no Array column type in MySQL. For these situations, I created a separate class and manually created associations.
There were certainly some parts of trial and error but on the whole it has worked quite well. The only part I couldn't complete programatically was associating uploaded images and documents back to the relevant products however due to the file naming structure for those uploads, it won't be a huge amount of manual effort to create the associations.
For those who are interested this is the gist of the file I used.

SQLAlchemy, how does it connect to an existing MYSQL database created in mysql workbench?

I am trying to create simple GUI python applications for populating an existing MYSQL database. Should I use sqlalchemy to connect to the server? I'm just wondering how Alchemy can connect to the existing DB which was created using MYSQL workbench?
Usually I create the database and tables using sqlalchemy that's why I can query and edit it. But for this case, the DB is already created. I guess the question would be how do I create the SQLAlchemy code of the existing DB?
Sorry for the lack of better words to explain my problem. I'm not that familiar with Database.
You can use sqlacodegen to generate python code for models in an existing database. You can also use SQLAlchemy's built in ability to reflect the tables or even map them to classes to dynamically load everything at runtime. SQLAlchemy doesn't care where the database came from, just where it is and what it looks like.
To connect to existing table in your database using SQLAlchemy it is easy even if the tables are not created using SQLAlchemy.
For this you need to use flask framework and flask-sqlalchemy
app = Flask(__name__)
app.secret_key = "any key"
db = SQLAlchemy(app)
app.config["SQLALCHEMY_DATABASE_URI"] = 'mysql://root#localhost/database_name'
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.init_app(app)
Classes need to be created to map these to your tables in the database .
Say for example database has a table named STUDENT for this table the class will be something like this
class Student(db.Model):
__tablename__ = "STUDENT"
id= db.Column(db.Integer , primary_key = True , nullable=False)
name = db.Column(db.Integer)
address= db.Column(db.String)
grade= db.Column(db.String)
year = db.Column(db.Integer)
To get the SQLAlchemy data types : https://docs.sqlalchemy.org/en/13/core/types.html
Similarly other classes can be created for other tables as well.
And to perform any operation on the data the class object type has to be used.
Say you wanted to get the student list
stu_obj = Student.query.all()
This will give you the list of all the rows in the student table.
For SQLAlchemy ORM queries
: https://www.tutorialspoint.com/sqlalchemy/sqlalchemy_orm_declaring_mapping.htm

web2py dal howto define model at runtime

I am a newbie to wewb2py,is it possible to create at runtime a model of a legacy database, for using DAL with it? I saw that there are some scripts that create the model file, but I do not know whether it is correct to put this file in the model directory of my application, I think not, I did some experiments, I can connect to the database with DAL querying its tables and for every table I can get the definition of the fields, the I tried to define the table with define_table,it works but try to create the table on the database and return an error because the table already exists; this is the relevant part of my code:
conn_string = "mysql://{0}:{1}#{2}/{3}".format(user,pwd,host,db_name)
db = DAL(conn_string)
db.define_table('test1',Field('prova','string'))
it works only the first time, when the table test1 does not exist yet on the database, I do not need to create the tables only work with their data, can you put me on the right way?
db = DAL(conn_string, migrate_enabled=False)
The above will prevent web2py from doing any migrations, including attempting to create any tables.

ForeignKey and ManyToMany without db relation

I have a question - is it possible to create a model with some relations, but when syncing database through manage.py syncdb not to create database relations? Just for example to have many-to-many tables and handle it through django ORM but without db foreign keys?
Not by default, you could of course implement your own database Field which stores an integer pointing to the specific record. This could then used to lookup a model for that type, but you throw away all the checking a database does for you.
Is there any specific reason why you want this?