If i create tables with sequelize API (sequelize.define), it returns a model object (User in the following example) that i can use to do queries (User.find) and other kind of operations:
var User = sequelize.define('User', {/* ... */})
If i need to create a table in the db without that api, but i need to do it with a pure sql query, is there a way to retrieve the same model object for my manual table and use it like the others?
In sequelize.models i see all my tables but not the custom one.
You need to define a model using define with the tableName property set to your manually defined table.
The DB columns that you want to retrieve as a model's attributes must meet a number of criteria. Each should have a datatype that matches the column's type. They should have the same constraints (cascade, null etc) as the DB columns. The column name should either match what Sequelize automatically generates or be specified manually using field.
Since the table for this model has already been manually created, make sure that it is not sync'ed to the DB. To test that the model you have specified will work the manually specified table you should sync the model to a test database and compare the automatically generated SQL with the manually generated SQL You are using.
This answer is based on the documentation here.
Related
The Model.rawAttributes returns the list of fields defined in sequelize.define() method. However, I want to check the fields for a particular model once the table has been created in the database. How do I do this?
I'm building a mongoDB database that holds sales data from multiple different systems. Each system is integrated via an node/mongoose/Express API that I'm creating for the database. Typically, you'd check the id to determine if a record already exists, and insert it if it doesn't. But since the ID from these different sources could technically overlap, I need a system to make sure that a source can only update records that originally came from that source. So I've added a column called "external_ID" where the record id from the source is saved, and another column called "integration ID", which will be unique to the specific system that sends data. But for that idea to work, I'd need to update only if those two columns matches, and otherwise insert a new record. Is that possible with MongoDB, or am I approaching this wrong?
Thank you so much.
Use upsert on update(). It will creates a new document when no document matches the query criteria.
db.collection.update(<query>, <update>, { upsert: true })
You can find more detail at Upsert Behavior documentation
I have a table Called 'usertab' to store user details such as:
userid uuid,
firstname text,
lastname text
email text
gender int
image text
Most of the other tables contains userid as a field for referencing 'usertab',
but when I retrieve data from other table, I need to execute another select query to get user details.
So if 10,000 or more data retrieved, same number of select query executed for getting user details. This makes our system slow.
So we add usertab fields such as firstname,lastname, gender, image in other tables in addition to userid field.
So on data retrieval, the system become fast, but we faced another problem. If any changes in usertab table such as change in firstname, lastname, gender or image, we need to update other tables that contains user details. If we consider huge amount of data in other tables, how can I handle this?
We are using lucene index and C#.
Cassandra writes significantly faster and more efficient than reads.
That's why cassandra prefer Denationalization over normalization
Denormalization is the concept that a data model should be designed so that a given query can be served from the results from one row and query. Instead of doing multiple reads from multiple tables and rows to gather all the required data for a response, instead modify your application logic to insert the required data multiple times into every row that might need it in the future This way, all required data can be available in just one read which prevents multiple lookups.
When executing multiple update you can use executeAsync.
Session allows asynchronous execution of statements (for any type of statement: simple, bound or batch) by exposing the ExecuteAsync method.
//Execute a statement asynchronously using await
var rs = await session.ExecuteAsync(statement);
Source : https://www.hakkalabs.co/articles/cassandra-data-modeling-guide
I think this is not possible, but however I ask the question, maybe I have missed something.
Can we add/remove columns from an azure table?
For example by default we get those columns: PartitionKey, RowKey, Timestamp, ETag. Can I add for example another 3: FirstName, LastName, Email columns?
After that I will insert some values and I want to remove column Email and add instead column Address. Can we do this?
As Igor rightly said, Azure Tables do not have the concept of rows and columns. A table can contain zero or more entities and each entity can have a maximum of 255 attributes (an attribute is a name/value/type). Off of these 255 attributes, 3 of them are system attributes (PartitionKey, RowKey and Timestamp) which you can't update through your code. When creating an entity you define PartitionKey and RowKey and after that they become readonly properties. So when it comes to updating an entity, you can only update 252 attributes.
To manage data in an Azure Table, there's a REST API and you provide attributes of an entity in the request body. Azure Storage provides two ways by which you can update an entity - Update and Merge.
When you tell Azure Table Service to Update an entity, it simply drops all the existing attributes for that entity, and inserts the attributes defined in the request payload.
When you tell Azure Table Service to Merge an entity, it looks at the existing entity attributes and compares them with the attributes defined in the request payload. If it finds matching attributes, it simply updates those attributes. If the attributes are not present in existing entity but are defined in request payload, those attributes get added to the entity. If the attribute are present in existing entity but are not defined in request payload, they are not changed.
Now coming to your problem.
So let's say you already have an entity where you just defined PartitionKey and RowKey. Now you want to add FirstName, LastName, Email attributes. Because these attributes are not there in the entity, you can either use Merge or Replace to update the entity and these attributes will be added to the entity.
Now you want to drop Email attribute from an entity and instead add Address attribute to that entity. What you will do is perform an Update operation on that entity where your request body will have FirstName, LastName and Address attributes only (no Email attribute). When you update the entity with this request payload, Email attribute will be removed from the entity.
Azure Tables do not have "columns" as SQL tables do. Azure Tables have entities. Each entity has up to 255 properties. Most tools that let you look at an Azure table, choose to visualize the data in it tabular with columns. However, in reality, each entity (row) is a collection of properties.
Therefore, you can save objects/entities into an Azure Table with different properties, everytime you do a save. It makes things somewhat confusing, but you can do it.
HTH
I think programmatic answers given above is the best solution in terms of achieving the functionality.
But in case, you are working in the development mode and you have inserted an unnecessary attribute which you want to remove. you can do the same by using azure storage explorer.
You can export the table of choice, delete the property in the CSV file and import it back in new table. drop the existing table and rename the new table to the existing one. This is kind of a work around.
I want to know that How to Insert New Columns to an existing database Table using Subsonic 3.0.0.5 MIGRATIONS.
Basically I want to alter an existing table in MS SqlServer database and add three more columns into it.
Please tell me How I will be able to do it
Regards,
Naveed Khan
Just change your object and the column will be added/updated whatever. So if you have an object called "Post" and add a property, it will be added as a column in the DB.
See this video...
http://subsonicproject.com/docs/Simple_Repo_5_Minute_Demo
It has to do with the conventions of SubSonic.
As the object is singular, it adds the plural to the table (or expects the table to be plural).
So it will expect an object called Order to map to a table called Orders.
There is only two solutions that I can see for you
1) Rename you table to the plural name.
2) Modify Subsonic code to remove the adding of the plural.