I am trying to pick up SQLalchemy and run constantly run into the following problem:
is there a way to retrieve some sort of a metadata string from session.commit(), which contains the current primary id SQLalchemy just created ?
You can simply get it from the SQLA object following its commit:
my_record= MyTable(id=None, FieldA='testing')
cs.add(myTable)
cs.commit()
print(my_record.id) # will be the primary id assuming it is autoincrement
Related
I am trying to create mysql table using groovy domain class. I have one master table and other table with has reference to field in the master table.
Let me explain more clearly. I have a master table
QualificationMaster
`````````````````
QualificaitonID
QualificationName
QualificaitonDuration
UserQualificationMap
```````````````````
Username
Email
QualificationID (this field refers to QualificationID in QualificationMaster)
Please help me in getting this done by using groovy domain class with sample snippet...I searched a lot but I find it so confusing..please help me as i am very new to groovy and helps me a lot. I am using GGTS IDE for this.
Check this out:
class QualificationMaster{
String QualificationName
Integer QualificaitonDuration
}
class UserQualificationMap{
String Username
String Email
QualificationMaster Qualification
}
You do not have to use QualificationMaster.QualificaitonID as primary key for QualificationMaster. QualificationMaster.id is created by default for each domain class (you can check it in your db).
Therefore you can make a reference to QualificationMaster from UserQualificationMap. It will be mapped as QualificationMaster's primary key in UserQualificationMap table.
Moreover try to use shorter and lowercased names for properties in your domain classes. For example change QualificationMaster.QualificationName to QualificationMaster.name and QualificationMaster.QualificaitonDuration to QualificationMaster.duration.
I have two tables which are Many-To-One mapped. However, it is important to maintain the order of the second table, so when I use automapping, Fluent automapper creates a bag. I changed this to force a list by using this command:
.Override(Of ingredients)(Function(map) map.HasMany(Function(x) x.PolygonData).AsList())
(VB.NET syntax)
So I say "AsList" and instead of using a bag, the mapping xml which gets generated contains a list now. Fine so far. However,
the statement generated cannot be handled by MySQL. I use MySQL55Dialect to create the statements and I use exactly that version. But it creates the following create:
create table `ingredients` (
Id INTEGER NOT NULL AUTO_INCREMENT,
Name FLOAT,
Amout FLOAT,
Soup_id INTEGER,
Index INTEGER,
primary key (Id)
)
It crashes because of the line "Index INTEGER," but I don't know what to do here. Any ideas?
Thanks!!
Best,
Chris
I would suspect that Index could be a keyword for MySQL. To avoid such conflict, we can define different Index column name (sorry for C# notation)
HasMany(x => x.PolygonData)
.AsList(idx => idx.Column("indexColumnName").Type<int>())
At http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-index_.html, it says
Automatic ID Generation
The index operation can be executed without specifying the id.
In such a case, an id will be generated automatically.
In addition, the op_type will automatically be set to create.
Here is an example (note the POST used instead of PUT):
$ curl -XPOST 'http://localhost:9200/twitter/tweet/' -d '{
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}'
So based on my reading, if I run that query twice, it should only index one document, and the second one should return 409. But when I actually run it (on elasticsearch 1.3.2), it creates a document every time! What's going on, and how can I get it to index only if the document doesn't already exist, without specifying the document id?
You can't, if you don't specify an id a new guid will be generated. The create op_type means it knows it doesn't need to do an update since it has a new unique id.
You could checksum your data and set that to id, but that is a bad idea if the data every changes.
I'm working on Rails 3.2.9 app with ruby 1.9.3 and mysql as my DB . I want to retrieve a particular column data called 'no_of_tc'from a model named "excel_file' but i dont have the primary key/id of tat row . All i have is the filename.
tc_no = ExcelFile.find(35).no_of_tc gives me the result but i dont have id all the time
tc_no = ExcelFile.find_by filename: 'excel_name' gives the error "unknown method - find_by"
How can i get the required data without having the primary key?? and why am I getting unknown method error for 'find_by'
I am assuming :file_name is another attribute of ExcelFile class.
try:
tc_no = ExcelFile.find_by_file_name(#name_of_the_fie)
I have a problem inserting a related row through Entity Framework 5. I'm using it with RIA Services and .NET Framework version 4.5. The database system is MySQL 5.6. The connector version is 6.6.5.
It raises a Foreign Key constraint exception.
I've chosen to simplify the model to expose my issue.
LDM
Provider(id, name, address)
Article(id, name, price)
LinkToProvider(provider_id, article_id, provider_price)
// Id's are auto_increment columns.
First I create a new instance of Article. I add an instance of LinkToProvider to the LinkProvider collection of the article. In this LinkToProvider object the product itself is referenced. An existing provider is also referenced.
Then I submit the changes.
Sample code from the DataViewModel
this.CurrentArticle = new Article();
...
this.CurrentArticle.LinkToProvider.Add(
new LinkToProvider { Article = this.CurrentArticle, Provider =
this.ProviderCollection.CurrentItem }
);
...
this.DomainContext.articles.Add(this.CurrentArticle);
this.DomainContext.SubmitChanges();
NOTE :
At the begining Entity Framework inserts the product well. Then it fails because it tries to insert a row in the LinkToPrivder table with an unkown product id like the following.
INSERT
INTO LinkToProvider
VALUES(5, 0, 1.2)
It puts 0 instead of the generated id.
But if I insert a product alone without any relations the product id is generated in the database correctly.
Any help will be much appreciated !
Thank you.
I found the answer.
You need to bind the result from the stored procedure to the id column in the edmx model
So I have to modify my stored procedure to add an instruction to show the last instered id for the article table on the standard output.
SELECT LAST_INSERT_ID() AS NewArticleId;
Then I added the binding with the name of the column name returned by the stored procedure. Here it's NewArticleId.
It's explained here : http://learnentityframework.com/LearnEntityFramework/tutorials/using-stored-procedures-for-insert-update-amp-delete-in-an-entity-data-model/.