Add imported classes to metadata - import

All,
I'm importing a group of classes for sqlalchemy from a separate file. They define the tables on my DB (inheriting from declarative_base()), and were originally located in the same file as my engine and metadata creation.
Since I have quite a few tables, and each of them is complex, I don't want them located in the same file I'm using them in. It makes working in the file more unwieldy, and I want a more clear delineation, since the classes document the current schema.
I refactored them to their own file, and suddenly the metadata does not find them automatically. Following this link, I found that it was because my main file declares base:
from tables import address, statements
Base = declarative_base()
metadata = MetaData()
Base.metadata.create_all()
And so does my tables file:
Base = declarative_base()
class address(Base):
...
So, as far as I can tell they get separate "bases" which is why the metadata can't find and create the declared tables. I've done some googling and it looks like this should be possible, but there isn't any obvious way to go about it.
How do I import tables defined in a separate file?
Update:
I tried this, and it sort of functions.
In the tables file, declare a Base for the table classes to import:
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
Then in the main file, import the preexisting base and give its metadata to a new Base:
from sqlalchemy.ext.declarative import declarative_base
from tables import Base as tableBase
Base = declarative_base(metadata=tableBase.metadata)
After some more testing, I've found this approach leaves out important information. I've gone back to one file with everything in it, since that does work correctly. I'll leave the question open in case someone can come up with an answer that does work correctly. Or alternatively, point to the appropriate docs.

Related

Do classes necessarily have to exist prior to record insertion?

I'm coming from Neo4j and evaluating OrientDB, and I had a quick question about classes - do they have to exist prior to inserting a record? That is, in Neo4j there's the 'MERGE' command, which will update or create a node if it doesn't exist. Classes seem roughly equivalent to Neo4j's labels, and if a label doesn't exist when performing a MERGE, it will be created. Is there similar functionality in OrientDB? Currently when I try to insert a record whose class doesn't exist, OrientDB throws an exception, "Class SOME_CLASS not found in database".
I've been reading through the docs trying to get a handle on the various data models available, but I can't find anything explicit on this issue. One thing I tried was to add a cluster, and then insert a record with a class that does not exist. This worked, and in OrientDB Studio, under 'DB', I see the cluster with number of records equal to '1'; however, the class of that new record does not appear under 'Schema'.
If dynamic class creation of this sort isn't possible, is it acceptable to check if a class exists in the schema, and if not, create it, and then proceed with creating the record? A different question is, if it's acceptable, is it good to do this, or should I always define the schema manually?
If you use one of the CREATE commands, then the object is placed in a default class; for example:
CREATE VERTEX
Created vertex 'V#9:0 v1' in 0.047000 sec(s).
(In this case, the class is V.)
And of course if you use the INSERT INTO ... form, then you have to specify a class.
So perhaps your first question boils down to whether it is possible to change the class of an OrientDB vertex or edge.
It is possible to change the parent class of a vertex -- see
http://orientdb.com/docs/2.1/SQL-Move-Vertex.html -- but there are important caveats.
To test whether a class exists programmatically, see e.g. this SO entry:
Check if class exists or not in orientdb
This gives a Java example, but a similar approach is possible using other supported languages.
As to the wisdom of changing the class of an entity dynamically -- perhaps the safe answer is that if you can achieve whatever you want using a property label, then use labels.

Does SQL Table name === Class name? and vice-versa?

So I'm just learning Cache, and have a question about tables, classes and globals, what's the difference?
I'm doing the tutorial and it seems like, since this is an object-orientated DB that a class is a table and vice versa. So if I create a class and make it persistent with properties, I can use SQL to query it. Is this true? What's a global then?
Reason I ask is because I'm using Management portal for one of our Cache applications and although I can see a table in WinSQL and Documatic, that same table doesn't seem to exist in the class explorer (Under management portal)... can't figure it out, is it hidden? Is there a command to see class def'ns in terminal??
thanks!
In Caché, classes are tables and tables are classes. You can choose when you want to use SQL access and when you want to use Object Oriented access.
Globals are the sparse multidimensional arrays which sit as storage beneath the class/table. Look at the storage definition at the end of your class to see the actual globals that your persistent class is stored in (e.g. ^Sample.PersonD)
By default, class names are projected as table names, but there are some rules which apply in order to ensure that the table names comply with SQL standards:
if you have a class that is a couple of packages deep (e.g. MyApp.Data.Person) then all but the last "." will be replaced by "_" in the table name (e.g. MyApp_Data.Person would be the table name)
You can't use reserved SQL keywords in table names, so if you make something in the User package for example (class: User.Person) then Caché will change the Package name (e.g. SQLUser.Person would be the name of the table)
I suggest you refer to http://docs.intersystems.com/cache20141/csp/docbook/DocBook.UI.Page.cls?KEY=GORIENT_ch_persistence for more detail

Telosys : How can i get database table records in template?

I am using Telosys tools for code generation. It's very nice tool and help me a lot.
But there is one problem, that is, it provides database schema information and i can access in templates (templates are formerly velocity templates), which is good, but how can i get selected entity's data from database? There is no way i can find, by which i can get that selected table data.
Please provide solution if any, or provide alternate way to do it.
Thanking You!
Telosys Tools is designed to retrieve the model from the database,
not the data stored in the tables.
But it allows to create your own specific tooling classes usable
in the templates, so it's possible to create a specific Java Class to retrieve the data from the database.
There's an example of this kind of specific class in the "database-doc" bundle
https://github.com/telosys-tools/database-doc-bundle-TT210 ( in classes folder )
To simplify the loading the simplest way is to create the class in the "default package" (no java package)
NB:
The problem is that the jar containing the JDBC driver
is not accessible by the generator class-loader, so you will have to use a specific class-loader and to connect directly with the JDBC driver.
Here is an example : https://gist.github.com/l-gu/ed0c8726807e5e8dd83a
Don't use it as is (the connection is never closed) but it can be easily adapted.

Programmatically import stored procedures in T4 in EF 5

I have a T4 template that generates the edmx model of my DB; I have to use this T4 template because i have to define my own localized pluralization dictionary to manage plurals/singulars.
The bad thing is that i have to manually import my stored procedures every time, after i launched my T4 template, via the 'import function' menu.
Is there a way to import the stored procedures programmatically?
Is the code of the "import function" wizard available and open source?
Mostly i'd like to see how they implemented the 'obtain column informations' to generate the result class.
The EDMX file is just XML. You can programmatically open and edit that XML file as you would any other.
I would imagine that "Obtain column information" works by using ADO to query the parameters, (see http://support.microsoft.com/kb/174223 ), populating them with dummy values, and querying the result - I think that would work, but perhaps SQL profiler would reveal more.

Imported a sproc in EF4 but no code was generated

I have a stored proc in my database and I am using Entity Framework 4 to work with the database from my code. Following instructions for importing a sproc, I was able to add it to my model and import a function to reference it. In the model browser under Function Imports, the stored proc and all its parameters appears. The access says "Public." Yet, I am not able to see it in Intellisense when I go to call it in my context object. When I do a global search on the name of the sproc, I see it appears in 3 places in the EDMX, and once in my calling code, but no code appears to have been generated for it in the Designer.cs file.
I must have missed a step or something, but as I look back through it, it's all pretty simple; I can't see where I went wrong. Has this happened to anyone? How can I troubleshoot?
if you switch to the model browser view on the .edmx file, you can see your imported SProcs under the model store node in the stored procedures folder. You can right-click them there and select "add function import" to generate the execution code in the entity data model's Designer.cs file. Unfortunately, you seem to have to do this import for them individually. If someone knows how to import them all simultaneously, I'd love to know.
Is that what you're looking for?
Take a look at this MSDN thread. On this forum, there is another thread addressing this issue in some way.
In short, it is an EF Designer limitation. You can try writing your own code to call this function, or use third-party designer like Entity Developer.