json – Unprocessable Entity: The following relationship fields were provided as attributes

As the error indicates, the entity reference has to be set in relationships, not attributes.

The documentation gives this example:

  "data": {
    "type": "node--article",
    "attributes": {
      "title": "Article by admin",
      "body": {
        "value": "Custom value",
        "format": "plain_text"
    "relationships": {
      "uid": {
        "data": {
          "type": "user--user",
          "id": "{{UUID of user 1}}"

In this example, the author of the node is set using the relationship of uid. However, it wasn’t immediately clear to me how to set the entity reference field.

It turns out to be quite easy:

const data = {
  data: {
    type: 'node--page',
    attributes: {
      field_text: {
        value: `${values.textFieldValue}`,
        format: 'basic_html',
    relationships: {
      field_ref_term: {
        data: {
          type: 'taxonomy-term--my-vocabulary',
          id: '9fa0cdc7-384c-4aa4-8ce9-d6c97fc2f44b',

The key for the relationship is the machine name of the entity reference field.

postgresql – Improving database design, is this a valid case for entity attribute values?

Well currently we are not using eav, however our database seems a bit finicky and one suggestion was something along the lines of entity attribute values.

What we have is a table which acts as “translations”. It has multiple rows with each line describing a translation. With multiple column, each column being a language.

CREATE TABLE table_name (
    id serial,
    organisation int,
    key text,
    en_gb text,
    nl_nl text,

This works fine (omitted constraints). However one feature is that users need to supply their own translations for data they inserted. In whatever language they wish. (Given the language needs to be formally described in iso 639-1)

This creates the weird situation where upon inserting not only a new row might be added. But the database schema also might be modified: a check is performed if the language column exists, if it is not it is added.

Which also adds a lot of columns (with nulls) for all fields – a minor problem though.

One solution would be to instead of having a column for each language use a json “translations” so that not each field/row needs to have a value for everything. This was dismissed on grounds that the far majority of the time we modify a single field, in an already existing column: apart from 6 or so languages where we operate we do not expect many more to be inserted.

Another solution was to use EAV, with instead of using a single row for all translations from a single field there is a new row for each language/translation of a certain field.

CREATE TABLE table_name (
    id serial,
    organisation int,
    key text,
    language text,
    translation text,

This seems a good case for entity attribute values right? Apart from that the database now is quite convoluted and isn’t easily visualized as a neat table. So is it?

The ability to visualize as a neat table I personally like since insertion happens most often by providing a csv, so the visualization mirrors that.

9 – View to get information for the user entity reference using contextual filters

I have two content types and each one has a user entity reference field. I want a view using contextual filter that shows the information of the content types when I pass the user ID referenced for the user entity reference field.

content-type 1: field -> studies, field -> id-user-entity-reference
content-type 2: field -> experience, field -> id-user-entity-reference

I want to get for example studies and experience in a view when I have a path similar like that:

/content/id-user-entity-reference (not the logged user, not the author id of the content type)

I need these results can be exposed via rest.

How can I configure my view to achieve that?

emails – identify author of an entity

Using D8 and hooks, I am able to send automatic to the current user email thanks to
BCC For drupal mail function

So, I define my message in hook_entity_update, and it is sent in hook_mail.

But I did not succeed to send a mail to someone else of the current user.

Here is the situation:

  1. user U1 consult a content created by user U2 (the aim is to permit to U1 to give his impression to U2 thanks to a select field). When U1 gave his impression, content is registered.
  2. At this step, I want to send a emal to U2 (author of this content), but since he is not the current user, I canot use “Drupal::currentuser” to retrieve the email.

So, my question is: in hook_entity_update, How to identify author of the current entity, knowing that “author” is not provided by “entity” (a DrupalCoreEntityEntityInterface) ?

Is there a specific service?


c# – In the absence of code-contracts, what techniques exist to represent state invariants (e.g. “Customer with Orders loaded” with Entity Framework)?

  • In Entity Framework 6 and/or Entity Framework Core 3+, the code-first types generated by the scaffolding (or other code-generation tools, my preference is this T4 script) are mutable classes that do not expose their Entry<T> state.

  • In web-applications today, current practice is to load the required entities inside the Controller’s Action and compose a View Model object, then pass the View Model off to the View. When the Controller’s Action method returns the DbContext is disposed, so the View Model must be fully-populated and thus cannot depend on any lazy-loading to render the View.

    • (btw, this question is not concerned with Model Binding or anything like that)
  • The problem is that if the View Model class simply embeds Entity Types (which is fine when the View Model is not being used in a <form>!) the View has no way of knowing what data is “loaded” and what it isn’t – and there’s also no easy way in C# to use the type-system to express these kinds of invariants about an object’s state.

For example, consider these mutable entity types (in .NET 4.8 – so we can’t use nullable-reference-types):

namespace Contoso.Data
    class Customer
        public Int32  CustomerId { get; set; }
        public String Name       { get; set; }

        public ICollection<Order> Orders { get; set; }

    class Order
        public Int32    OrderId    { get; set; }
        public Int32    CustomerId { get; set; }
        public Customer Customer   { get; set; }

And this immutable View Model for a page which is a read-only view of a Customer’s Orders:

namespace Contoso.Web
    class CustomerOrderPageViewModel : IPageViewModel
        String IPageViewModel.PageTitle => this.CustomerSubject.Name + "'s Orders";

        public CustomerOrderPageViewModel( Customer c )
            this.CustomerSubject = c ?? throw new ArgumentNullException(nameof(c));
            if( c.Orders is null ) throw new ArgumentException( "Orders is null." )

        public Customer CustomerSubject { get; }

And this straightforward Razor view’s fragment:

@foreach( Order order in this.Model.Customer.Orders ) {

The view expects that CustomerOrderPageViewModel.CustomerSubject contains a Customer with a loaded Orders collection (not to mention not-null too). It is not possible for the View to statically express its expectation that the collection is loaded (this would be possible with C# Code Contracts, but that’s dead in the water right now – I’d love to see it come back, however).

The “solution” is to move the Orders collection to the CustomerOrderPageViewModel so it at least that expresses the expectation that Orders is available – and as CustomerOrderPageViewModel is immutable and the .Orders collection is only set by the constructor that makes it “safe” as far as I’m concerned – but this introduces a lot of time wasting by essentially copying the data’s structural definition from the Entity type to the View-Model type – which doesn’t “scale” when an application could have hundreds of different views all with similar requirements. My job is in the business of automating away tedious things – so I’d rather not have tedious things to do myself!

Another problem is that the View (and View-Model) has no way of knowing if an empty CustomerSubject.Orders collection represents a either a collection that hasn’t been loaded yet (e.g. due to a bug/missing-step in the entity loading code) – or if it did actually load the collection but the Customer has not made any orders.

So I’ve been thinking about how we could use immutable types in C#/.NET to express invariants, and how as I’m already using a very customizable T4 template to scaffold entity types I can extend that T4 to generate state invariant types to represent the shape of loaded data.

An example based on the scenario above would involve a new type struct CustomerWithLoadedOrders, like so (it’s a struct because invariants shouldn’t participate in an inheritance hierarchy, they’re immutable, and we should avoid GC heap allocations anyway):

struct CustomerWithLoadedOrders
    public static implicit operator CustomerWithLoadedOrders( ( Customer c, IReadOnlyList<Order> o ) t ) => new CustomerWithLoadedOrders( t.c, t.o );
    public static implicit operator Customer( CustomerWithLoadedOrders self ) => self.Customer;
    public static implicit operator IReadOnlyList<Order>( CustomerWithLoadedOrders self ) => self.Orders;

    public CustomerWithLoadedOrders( Customer c, IReadOnlyList<Order> loadedOrders )
        this.Customer     = c            ?? throw new ArgumentNullException(nameof(c));
        this.LoadedOrders = loadedOrders ?? throw new ArgumentNullException(nameof(loadedOrders));

    public Customer             Customer     { get; }
    public IReadOnlyList<Order> LoadedOrders { get; }

So then I’d have an extension-method on the DbContext:

public static async Task<CustomerWithLoadedOrders> LoadCustomerWithOrdersAsync( this MyDbContext db, Int32 customerId )
    Customer cus = await db.Customers.SingleAsync( c => c.CustomerId == customerId ).ConfigureAwait(false);

    await db.Entry( cus ).Collection( c => c.Orders ).LoadAsync().ConfigureAwait(false);

    return ( cus, cus.Orders ); // or just `new CustomerWithLoadedOrders( cus, cus.Orders )`

This looks tediuous to write my hand, but with T4 invariant types can be easily generated for each loadable member of every entity type, and T4 can also be used to generate combinations of invariants together, think of it as a worse-than-poor-man’s implementation of an ADT product type (with most of the fiddly syntax pain removed and compatibility with entity types maintained with implicit conversion).

…and this works for the simpler cases involving single types or collections-of, but eventually it results in massive code-bloat (even if it is generated code) when needing to define invariants for the state of a any moderately-sized object-graph: for example, a Customer, all their Orders, as well as all Products in all of those Orders – and it quickly spirals from there. An additional pain-point is that for batch loading or customized Linq queries we can’t use extension methods like LoadCustomerWithOrdersAsync because those load single entities – so we need to rely on runtime assertions which defeats the point of using a static-type system to encode invariants in our program.

Have you run into this problem, and how did you come to a solution?

entities – add Bundle field to a custom entity

I am trying to add a new bundle field into my custom entity, I added the following code into the MyCustomEntity.php file, I uninstalled my module, so, I installed it again, the field is showing up on the view, but in my data base the field is not added.

Could you please support me?

$fields('new_name') = BaseFieldDefinition::create('text_long')
  ->setLabel(t('New Name'))
  ->setDisplayOptions('form', (
    'type' => 'text_textarea',

Thank you, I will really appreciate your support.

database design – Entity Relationship Model

Draw an ER-Model for the following info that can appropriately record the info described :

Question : An Airport has a city name and International identification code (Eg: EDDH for Hamburg Airport). A fight is identified by its flight no (Eg: LH4324). A flight has a starting airport, a destination airport and a departure date. Further more, a fight has three prices (first, business, economy) and a status (not yet started, started, landed).

Can anyone on this forum solve this question for me. It will be grateful for me.

c# – In EF Core 5, I have entity with a many to many relation with another table. How can I insert a parent entity by setting only the foreigns keys IDs?

The other table contain references data with well know ID. The use case is to read data from file, create entities then insert them in batch. I don’t need to query anything first, so all entities are “disconnected” from context.

Simple exemple:

public class Post
    public int ID { get; set; }
    public string Text { get; set; }
    public virtual ICollection<Tag> Tags { get; set; } 

public class Tag
    public int ID { get; set; }
    public string Label { get; set;}
    public virtual ICollection<Post> Posts { get; set; } 

First try

List<Post> posts = new List<Post>();

  var post = new Post { Text = "some text"});
  post.Tags.Add(new Tag {ID = 1});  
  post.Tags.Add(new Tag {ID = 2});


Error because EF try to update the tags record by setting the other tag column to null. I don’t want EF to update the tag table anyway, only the join table.

Second try
After reading Long story short: Use Foreign key and it will save your day, I did not find a way to make it work with a collection of FKs because in my case it’s a many to many relation.

Third try
Instead of using context.Post.AddRange(posts);, I attach only the parent entity:

var post = new Post { Text = "some text"});
post.Tags.Add(new Tag {ID = 1});
post.Tags.Add(new Tag {ID = 2});
context.Posts.Attach(post).State = EntityState.Added;

That worked. Post is inserted, and the joining table PostsTags contain the relation data, with the Tags table left untouched.
BUT that will not work in batch (same context) because I can’t then create another post with the same tag. The context “track” the tags by their ID, so I can’t insert a “new” one with the same ID.

Fourth try
What I’m doing right now is instead of adding a new Tag post.Tags.Add(new Tag {ID = 1});, I add the tag from the db post.Tags.Add(context.Tags.Find(1)); That means many trips to database, for information that is already knows.

Others options I think of is to keep a local dictionnary of tag that are already attached to context, change context between each post, find a way to insert data directly into the entity type that represent the join table, query all references beforehand (but some references tables contains thousand of elements) or simply juste use raw sql query.

I can’t imagine that there is no simple way to insert a model with Fk ids, like it work for a one to many by using a Foreign Key property.

Thank you

java – related entity loads when called Spring Boot

I have a problem with the spring boot, that it doesn’t load the UserRole entity when I give the User a findByUsername.


@Table(name = "SFT_USER")
public class User extends AbstractEntity {

    @SequenceGenerator(name="id_seq_sft_user", sequenceName = "seq_sft_user", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_seq_sft_user")
    private Long id;

    @NotEmpty(message = "Name is required")
    @Column(name = "NAME")
    private String name;

    @NotEmpty(message = "Login is required")
    @Column(name = "USERNAME", unique = true)
    private String username;

    @NotEmpty(message = "E-mail is required")
    @Column(name = "EMAIL")
    private String email;

    @NotEmpty(message = "Password is required")
    @Column(name = "PASSWORD")
    private String password;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
    private List<UserRole> userRoles;

    ... Getters and Setters ...


@Table(name = "SFT_USER_ROLE")
public class UserRole extends AbstractEntity {

    @SequenceGenerator(name="id_seq_sft_user_role", sequenceName = "seq_sft_user_role", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_seq_sft_user_role")
    private Long id;
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "ID_USER", referencedColumnName = "ID", nullable = false)
    private User user;
    private EnumUserRole userRole;
    private Date dtRegister;

... Getters and Setters ...


public interface UserRepository extends JpaRepository<User, Long> {

    User findByUsername(String username);
public class Example {
    private UserRepository userRepository;

    public void testExample(String username) throws UsernameNotFoundException {
        User user = userRepository.findByUsername(username);
        List<UserRole> userRoles = user.getUserRoles();


Note: I tried to use hibernate.initialize but it didn’t work

postgresql – Database Design – Can’t figure out entity relationships for web application backend


I’m working on a personal project to replace a scheduling system at work. This is outside my regular duties and I’m hoping to be able to present a replacement web application to my supervisors that will help me transition to a development role. The current schedule is generated using a Microsoft Access database and a lot of custom code modules. The main issue outside of being Access and crashing a lot, is that it’s not flexible. Anytime it needs to be updated (at least twice a year), it requires someone with programming knowledge to get into and make many changes to the underlying modules. My goal is create something that anyone can keep updated through a web interface.

I’m using .NET Core for the application and PostgreSQL as the backend because it seems to work well with Entity Framework Core. However, this is the first database I’ve ever designed myself, and I don’t know the best way to set it up.

Original Design

Here is my first attempt at an ER diagram. I actually have a working application using this, but a lot of the schedule fill logic is convoluted and as I continue to add features it has become difficult to work with. One of the major problems is the code to iterate through the work stations assigned to each work center and the fact that crews don’t have a parent relationship which I’ve realized was a design oversight.

Original Entity Relationship Diagram

Additionally, I was forcing the schedule into an easy to display table like this (also including the rest of the days):

SchedID WorkCenter WorkStation Year Week MondayEmployeeId Tuesday Wed Thurs
1 Line 1 Crew Lead 2020 35 null 9999 9999 null

Current Working Design

Here is my current working entity relationship diagram.

Current working Entity Relationship Diagram

For this one, notice I have changed the schedule to create a separate entry for each date and work assignment. This gives me all the position information I need and the 1st/2nd half employee entries allow me to easily swap out employees for vacation and sick days and add replacements, but I will eventually need to put it back into the easy to read format. I’m thinking a query will work for that. We have a few hundred employee positions (work assignments) that will have to be reorganized for each work week. I’m a little worried about how fast the table will fill up, figure 300 rows per day more or less or roughly 110000 per year. Those numbers would scare me with Access but PostgreSQL should be fine.

The middle section is pretty straightforward. The top level is Division, divisions have plants, plants have departments, departments have work centers and work centers have work stations (i.e. the machines or areas that workers are performing their duties). Work stations are mostly static, only changing if a station breaks and needs to be completely replaced.

Then things get confusing. Crews exist (A, B, C, etc.), but I’m not sure where they belong in the relationship. We refer to them by department typically (Shipping A crew, Support C crew, etc.) and that is the level they’re assigned supervisors.

Work centers are manufacturing lines (1-10). Each line has several crews, but may not operate on every crew. Crew A may have lines 1-6, Crew B lines 1 and 3-8, and Crew C lines 5-10. It depends on current staffing levels and the amount of work. Work center/crew combos change regularly.

For each work center/crew combo, there is an employee assigned to each work station. These employees are also shuffled around all the time.

The rotation is the “current schedule”, and refers to how long crews are on day shift then night shift. This is the schedule change that typically happens twice a year and causes all kinds of problems that I referred to above.

Work weeks are how I can tell what days each work center is working in a given week. For instance, our most common schedule is 6 weeks of day shift followed by 3 weeks of night shift. So I can calculate a number based on the week of the year and the crew and use that to check each day whether they’re working or not. The actual work week table has true/false fields for each day of the week, I just left them off the diagram to conserve space.

Each work center can have it’s own rotation. Line 3 might be on 2 weeks days 2 weeks nights while the rest of the lines are on the 6/3 rotation. This further confuses the crew assignments because a 2/2 rotation would have 4 crews while the 6/3 rotation would have 3. There are also rotations with only 2 crews. This makes me thing that maybe crew should be a child of work center even though when I try to visualize how the application works seems like a lot of duplication.

I’ve also considered making a Department has a Crew has a Work Center (with a many-to-many Crew<>WC relation). This seems like it would allow me to get rid of the work assignment table, but I’m not sure I understand that correctly and I’m worried it won’t work. I think I would need a one-to-many relationship between crew and work center and have to duplicate work centers for it to actually be correct.

Entity relatinship diagram with Department to Crew to Work Center relationships

Fill Logic

To actually generate the schedule, I am iterating through each level starting with department. I need both the rotation and the Crew to caculate the variable for the working week. Something like:

foreach (department){
    foreach (workcenter in department) {
        foreach(workStation in workCenter) {
            foreach(workAssignment in workStation) {
                foreach (dateInWeek) {
                    if (isWorkingDay(workAssignment.workStation.workCenter.Rotation, Crew) {
                        Create new Schedule(date, workAssignment)

Actually, that can probably be simplified to the following with my current working ER diagram.

foreach(workAssignment) {
    foreach (dateInWeek) {
        if (isWorkingDay(workAssignment.workStation.workCenter.Rotation, Crew) {
            Create new Schedule(date, workAssignment)

In any case, the fill logic for my new Schedule table idea is so much simpler than the original that I believe I’m on the right track. I will still have to figure out how to turn the Schedule data into a displayable table, but that seems doable at least.


I spent more than a month working on my original application in my free time. While it works, it’s not as easy to use and update as I need to present to my supervisors. Since I’ve decided to scrap the backend and start over, I’ve decided to get some other opinions from more experienced eyes to help.

I’m open to any suggestions. Hopefully my new ER design is workable, but if not I definitely want to know where I went wrong and how to fix it.

Thank you all and if you made it this far I apologize for making this so long.