Mapping a sql view to a php entity with doctrine2 - doctrine-orm

I have a view in my database created with
CREATE VIEW TBL_TITLE (...)
That view is described in yaml by
HQ\Title:
type: entity
table: TBL_TITLE
fields:
(...)
lifecycleCallbacks: { }
This works fine: my entities are loaded and written correctly. But when I run orm:schema-tool:update, I get
CREATE TABLE TBL_TITLE (...);
So doctrine2 (2.1.6) doesn't see that TBL_TITLE already exists as a view and wants to create a table. How can I declare TBL_TITLE as a view so that schema-tool recognize it?

You cannot, the schema tool isn't able to handle that at current state.

Related

AdonisJs: create migration for existent database schema

I have an existent Postgresql database schema which I wrote in SQL. It initializes extensions, enum types, and defines many tables with foreign keys, constraints, indexes, etc.
Now I wish to build an AdonisJs app around that database. I would like new developers to be able to set up an empty database with the same schema, without having to run the SQL command in psql themselves.
I do not want to write all the schema using the Adonis schema builder DSL because there are many tables and it would take a lot of time to translate.
In ruby on Rails, the schema would be kept updated in the db/schema.rb file and new developers would spin up their dev environment using rails db:schema:load. What is the equivalent in AdonisJS?
The current way I'm doing it is by creating a migration that basically runs the raw SQL query. But I'm convinced there should be a cleaner way. Or even a way to get this SQL file away from my migration.
import Database from '#ioc:Adonis/Lucid/Database'
import BaseSchema from '#ioc:Adonis/Lucid/Schema'
export default class extends BaseSchema {
public async up () {
const schemaSql = this.initialSchema()
console.log(schemaSql)
await Database.rawQuery(schemaSql)
}
public async down () {
}
public initialSchema() {
const fs = require('fs');
const path = require('path');
var data = fs.readFileSync(path.resolve(__dirname, './../initial_schema.sql'));
return data.toString();
}
}
and then inside database/initial_schema.sql I have:
CREATE EXTENSION IF NOT EXISTS citext;
CREATE EXTENSION IF NOT EXISTS pgcrypto;
CREATE EXTENSION IF NOT EXISTS btree_gist;
DROP TYPE IF EXISTS nivel_formacion;
CREATE TYPE nivel_formacion AS ENUM ('secundaria', 'grado', 'maestria', 'doctorado');
CREATE TABLE "foobar" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid(),
"name" varchar NOT NULL,
"created_at" timestamp NOT NULL,
"updated_at" timestamp NOT NULL
);
-- ...etc etc etc..

Create postgresql view in django

I want to use postgresql view to store intermediate result of queryset in django.
Is there a good way to create a view in django?
Below is the code of the flow I want!
elements = Element.objects.filter(is_active=True, eventcardad__isnull=False)
# save elements to database view
# reuse the query results by using view
You can use raw SQL query to do that read this page first.

Loopback 4: How to access table with underscore in name from loopback 4?

I am trying to get data from my data source using loopback 4. It is working fine if the table name is simple and does not contain any special character.
But if there is a table with some special character like Underscore it does not allow me to create a model for that and I am not able to access the data from that table.
I have a table named "my_data" that contains column:- id,first_name,last_name.
But when I use the command lb4 model and pass the model name as my_data it converts it to my-data. and later on, when I call the API it throws an error by saying that relation publi.mydata does not exist.
WARNING: relational database doesn't support {strict: false} mode. {strict: true} mode will be set for model MyData instead.
Unhandled error in GET /my_data?filter=%7B%0A%20%20%22fields%22%3A%20%7B%0A%20%20%20%20%22id%22%3A%20true%2C%0A%20%20%20%20%first_name%22%3A%20true%2C%0A%20%20%20%20%22additionalProp1%22%3A%20%7B%7D%0A%20%20%7D%0A%7D: 500 error: relation "public.mydata" does not exist
at Connection.parseE (/Users/apple/others/loopback4/hello/node_modules/pg/lib/connection.js:614:13)
at Connection.parseMessage (/Users/apple/others/loopback4/hello/node_modules/pg/lib/connection.js:413:19)
at Socket.<anonymous> (/Users/apple/others/loopback4/hello/node_modules/pg/lib/connection.js:129:22)
at Socket.emit (events.js:315:20)
at addChunk (_stream_readable.js:297:12)
at readableAddChunk (_stream_readable.js:273:9)
at Socket.Readable.push (_stream_readable.js:214:10)
at TCP.onStreamRead (internal/stream_base_commons.js:186:23)
Is ther any way to get data from table named like this? If anyone know how to do this please let me know.
Use settings.table in the #model decorator:
#model({
settings: {
table: 'my_data',
},
})
Further reading
https://loopback.io/doc/en/lb4/Model.html#data-mapping-properties

Find the class name of a relation from the instance of a model in ember JS

I have foo an instance of the ember-data model thing. thing.js has the following property :
owner: DS.belongsTo('user')
If I have foo with an empty owner, how can I, with only foo and the 'owner' string, retrieve the value 'user' representing the model of the owner relation?
EDIT: I want to allow my select-relation component to works with relations where the name is different from the class name
It sounds like you have some work to do to finish setting up your relationships. Have a read through this page of the guides.
If the relationships are set up correctly, to get the associated user, you should be able to do foo.owner. This assumes that users are already present in the store. I recommend using the Ember Inspector browser plugin to debug the relationships.
This looks like a use case for typeForRelationship.
In your example you should be able to do something like
store.modelFor('thing').typeForRelationship('owner', store);
If you don't like that approach you can use the belongsTo reference API, where you use the meta data from the relationship to get the type
foo.belongsTo('owner').type
The only thing with that approach is that the type property may not be public API and possible (though unlikely) to change at some point.
It seems I can do the following :
this.get('model').get('_internalModel._relationships.initializedRelationships.'+this.get('relation')+'.relationshipMeta.type')
model being an instance and relation the string of the relation name, it correctly return the model of the relation.
EDIT : a better solution not using private API courtesy from the ember discord :
function getRelatedModelName(record, relationName){
let ParentModelClass = record.constructor;
let meta = get(ParentModelClass, 'relationshipsByName').get(relationName);
return meta.type;
}

adding models to backbone collections

I have a rails app with 4 Model classes, with multiple instances in each table. I have created backbone Model and Collection classes in CoffeeScript to match. I can successfully load all of the collections and can render them in views. So far, so good. Here is one of my collections, with its associated model:
class window.CoffeeWater.Collections.Histories extends Backbone.Collection
url: '/api/histories'
model: History
class window.CoffeeWater.Models.History extends Backbone.Model
I need to be able to create a History model object, and then add it to the Histories collection. The documentation states that I have to set a 'collection' property when creating my new model, in order for it to get the 'url' property from the collection. My problem is that I can't seem to set the 'collection' property value correctly, because the url property does not get set on the model instance
attributes = {'start_time': new Date (1434740259016), 'stop_time': new Date (1434740259016 +(86400*1000)), 'valve_id': 2}
options = { collection: window.CoffeeWater.Collections.Histories }
history = new window.CoffeeWater.Models.History(attributes, options)
window.CoffeeWater.Objects.Collections.Histories.add(history)
Inspecting the resulting 'history' object does not show the same attributes that exist in models already in the collection, and the url property is missing.
I am currently at a loss. Does anyone have an example on how to do this? The backbone.js docs do not show any relevant examples.
The way the url in model is defined like this.
If there is a url property in model.
class window.CoffeeWater.Models.History extends Backbone.Model
url:'/api/history/1'
Then when model.fetch(), it will call that url. If this property is not found, it will see if the associated collection does have a 'url'. You try to set this collection variable.
What you are missing is this. This
class window.CoffeeWater.Collections.Histories extends Backbone.Collection
is actually a definition of a class. It is not an object yet. You need to initialize it, like you do in java. so
var historyCollection=new window.CoffeeWater.Collections.Histories()
Now you have a collection object. When you do
historyCollection.add(history);
The "collection" of model is automatically set to the "historyCollection" object, which contains a 'url'. So in fact, you do not need to manually put this in the option.
basically
attributes = {'start_time': new Date (1434740259016), 'stop_time': new Date (1434740259016 +(86400*1000)), 'valve_id': 2}
var historyCollection=new window.CoffeeWater.Collections.Histories()
var history = new window.CoffeeWater.Models.History(attributes)
historyCollection.add(history)